Index: factory/facFactorize.cc
===================================================================
--- factory/facFactorize.cc	(revision 4505ca15be27a1d26c6a46880debf164fc3d31b0)
+++ factory/facFactorize.cc	(revision 3163a2e6bdf16d80875b60e58d864a97a416776a)
@@ -430,10 +430,9 @@
                         biFactors, evaluation);
 
-  CFListIterator iter2, iter;
+  CFListIterator iter;
   CFList bufLeadingCoeffs2= leadingCoeffs2[lengthAeval2-1];
   bufBiFactors= biFactors;
   bufA= A;
-  CanonicalForm bufLCmultiplier= LCmultiplier;
-  CanonicalForm testVars;
+  CanonicalForm testVars, bufLCmultiplier= LCmultiplier;
   if (!LCmultiplierIsConst)
   {
@@ -506,24 +505,9 @@
       LCheuristic= true;
       factors= oldFactors;
-      CanonicalForm cont;
       CFList contents, LCs;
-      int index=1;
       bool foundTrueMultiplier= false;
-      for (iter= factors; iter.hasItem(); iter++, index++)
-      {
-        cont= content (iter.getItem(), 1);
-        cont= gcd (cont , LCmultiplier);
-        contents.append (cont);
-        if (cont.inCoeffDomain()) // trivial content->LCmultiplier needs to go there
-        {
-          foundTrueMultiplier= true;
-          int index2= 1;
-          for (iter2= leadingCoeffs2[lengthAeval2-1]; iter2.hasItem(); iter2++,
-                                                                    index2++)
-          {
-            if (index2 == index)
-              continue;
-            iter2.getItem() /= LCmultiplier;
-          }
+      LCHeuristic3 (LCmultiplier, factors, leadingCoeffs2[lengthAeval2-1], contents, LCs, foundTrueMultiplier);
+      if (foundTrueMultiplier)
+      {
           A= oldA;
           leadingCoeffs= leadingCoeffs2[lengthAeval2-1];
@@ -532,126 +516,19 @@
           prepareLeadingCoeffs (leadingCoeffs2, A, Aeval, A.level(),
                                 leadingCoeffs, biFactors, evaluation);
-          break;
-        }
-        else
-          LCs.append (LC (iter.getItem()/cont, 1));
-      }
-      if (!foundTrueMultiplier)
-      {
-        index= 1;
-        iter2= factors;
+      }
+      else
+      {
         bool foundMultiplier= false;
-        for (iter= contents; iter.hasItem(); iter++, iter2++, index++)
-        {
-          if (fdivides (iter.getItem(), LCmultiplier))
-          {
-            if ((LCmultiplier/iter.getItem()).inCoeffDomain() &&
-                !isOnlyLeadingCoeff(iter2.getItem())) //content divides LCmultiplier completely and factor consists of more terms than just the leading coeff
-            {
-              Variable xx= Variable (2);
-              CanonicalForm vars;
-              vars= power (xx, degree (LC (getItem(oldBiFactors, index),1),
-                                        xx));
-              for (int i= 0; i < lengthAeval2; i++)
-              {
-                if (oldAeval[i].isEmpty())
-                  continue;
-                xx= oldAeval[i].getFirst().mvar();
-                vars *= power (xx, degree (LC (getItem(oldAeval[i], index),1),
-                                           xx));
-              }
-              if (vars.level() <= 2)
-              {
-                int index2= 1;
-                for (CFListIterator iter3= leadingCoeffs2[lengthAeval2-1];
-                     iter3.hasItem(); iter3++, index2++)
-                {
-                  if (index2 == index)
-                  {
-                    iter3.getItem() /= LCmultiplier;
-                    break;
-                  }
-                }
-                A /= LCmultiplier;
-                foundMultiplier= true;
-                iter.getItem()= 1;
-              }
-            }
-          }
-        }
+        LCHeuristic4 (LCmultiplier, factors, oldBiFactors, contents, oldAeval, A, leadingCoeffs2, lengthAeval2, foundMultiplier);
         // coming from above: divide out more LCmultiplier if possible
         if (foundMultiplier)
         {
           foundMultiplier= false;
-          index=1;
-          iter2= factors;
-          for (iter= contents; iter.hasItem(); iter++, iter2++, index++)
-          {
-            if (!iter.getItem().isOne() &&
-                fdivides (iter.getItem(), LCmultiplier))
-            {
-              if (!isOnlyLeadingCoeff (iter2.getItem())) // factor is more than just leading coeff
-              {
-                int index2= 1;
-                for (iter2= leadingCoeffs2[lengthAeval2-1]; iter2.hasItem();
-                     iter2++, index2++)
-                {
-                  if (index2 == index)
-                  {
-                    iter2.getItem() /= iter.getItem();
-                    foundMultiplier= true;
-                    break;
-                  }
-                }
-                A /= iter.getItem();
-                LCmultiplier /= iter.getItem();
-                iter.getItem()= 1;
-              }
-              else if (fdivides (getVars (LCmultiplier), testVars))//factor consists of just leading coeff
-              {
-                Variable xx= Variable (2);
-                CanonicalForm vars;
-                vars= power (xx, degree (LC (getItem(oldBiFactors, index),1),
-                                          xx));
-                for (int i= 0; i < lengthAeval2; i++)
-                {
-                  if (oldAeval[i].isEmpty())
-                    continue;
-                  xx= oldAeval[i].getFirst().mvar();
-                  vars *= power (xx, degree (LC (getItem(oldAeval[i], index),1),
-                                             xx));
-                }
-                if (myGetVars(content(getItem(leadingCoeffs2[lengthAeval2-1],index),1))
-                    /myGetVars (LCmultiplier) == vars)
-                {
-                  int index2= 1;
-                  for (iter2= leadingCoeffs2[lengthAeval2-1]; iter2.hasItem();
-                       iter2++, index2++)
-                  {
-                    if (index2 == index)
-                    {
-                      iter2.getItem() /= LCmultiplier;
-                      foundMultiplier= true;
-                      break;
-                    }
-                  }
-                  A /= LCmultiplier;
-                  iter.getItem()= 1;
-                }
-              }
-            }
-          }
+          LCHeuristic5 (oldBiFactors, oldAeval, contents, factors, testVars, lengthAeval2, leadingCoeffs2, A, LCmultiplier, foundMultiplier);
         }
         else
         {
-          CanonicalForm pLCs= prod (LCs);
-          if (fdivides (pLCs, LC (oldA,1)) && (LC(oldA,1)/pLCs).inCoeffDomain()) // check if the product of the lead coeffs of the primitive factors equals the lead coeff of the old A
-          {
-            A= oldA;
-            iter2= leadingCoeffs2[lengthAeval2-1];
-            for (iter= contents; iter.hasItem(); iter++, iter2++)
-              iter2.getItem() /= iter.getItem();
-            foundMultiplier= true;
-          }
+          LCHeuristic2 (LCs, contents, A, oldA, leadingCoeffs2[lengthAeval2-1],
+                        foundMultiplier);
           if (!foundMultiplier && fdivides (getVars (LCmultiplier), testVars))
           {
@@ -751,4 +628,5 @@
     commonDenominators.append (bCommonDen (iter.getItem()));
   CanonicalForm tmp1, tmp2, tmp3=1;
+  CFListIterator iter2;
   for (int i= 0; i < lengthAeval2; i++)
   {
Index: factory/facFqFactorize.cc
===================================================================
--- factory/facFqFactorize.cc	(revision 4505ca15be27a1d26c6a46880debf164fc3d31b0)
+++ factory/facFqFactorize.cc	(revision 3163a2e6bdf16d80875b60e58d864a97a416776a)
@@ -2355,5 +2355,8 @@
 }
 
-void changeSecondVariable (CanonicalForm& A, CFList& biFactors, CFList& evaluation, CFList*& oldAeval, int lengthAeval2, const CFList& uniFactors, const Variable& w)
+void
+changeSecondVariable (CanonicalForm& A, CFList& biFactors, CFList& evaluation,
+                      CFList*& oldAeval, int lengthAeval2,
+                      const CFList& uniFactors, const Variable& w)
 {
   Variable y= Variable (2);
@@ -2399,5 +2402,8 @@
 }
 
-void distributeLCmultiplier (CanonicalForm& A, CFList& leadingCoeffs, CFList& biFactors, const CFList& evaluation, const CanonicalForm& LCmultipler)
+void
+distributeLCmultiplier (CanonicalForm& A, CFList& leadingCoeffs,
+                        CFList& biFactors, const CFList& evaluation,
+                        const CanonicalForm& LCmultipler)
 {
   CanonicalForm tmp= power (LCmultipler, biFactors.length() - 1);
@@ -2550,4 +2556,157 @@
 }
 
+void
+LCHeuristic2 (const CFList& LCs, const CFList& contents, CanonicalForm& A,
+              const CanonicalForm& oldA, CFList& leadingCoeffs,
+              bool& foundTrueMultiplier)
+{
+  CanonicalForm pLCs= prod (LCs);
+  if (fdivides (pLCs, LC (oldA,1)) && (LC(oldA,1)/pLCs).inCoeffDomain()) // check if the product of the lead coeffs of the primitive factors equals the lead coeff of the old A
+  {
+    A= oldA;
+    CFListIterator iter2= leadingCoeffs;
+    for (CFListIterator iter= contents; iter.hasItem(); iter++, iter2++)
+      iter2.getItem() /= iter.getItem();
+    foundTrueMultiplier= true;
+  }
+}
+
+void
+LCHeuristic3 (const CanonicalForm& LCmultiplier, const CFList& factors, CFList& leadingCoeffs, CFList& contents, CFList& LCs, bool& foundTrueMultiplier)
+{
+  CanonicalForm cont;
+  int index= 1;
+  CFListIterator iter2;
+  for (CFListIterator iter= factors; iter.hasItem(); iter++, index++)
+  {
+    cont= content (iter.getItem(), 1);
+    cont= gcd (cont , LCmultiplier);
+    contents.append (cont);
+    if (cont.inCoeffDomain()) // trivial content->LCmultiplier needs to go there
+    {
+      foundTrueMultiplier= true;
+      int index2= 1;
+      for (iter2= leadingCoeffs; iter2.hasItem(); iter2++, index2++)
+      {
+        if (index2 == index)
+          continue;
+        iter2.getItem() /= LCmultiplier;
+      }
+      break;
+    }
+    else
+      LCs.append (LC (iter.getItem()/cont, 1));
+  }
+}
+
+void
+LCHeuristic4 (const CanonicalForm& LCmultiplier, const CFList& factors, const CFList& oldBiFactors,
+              const CFList& contents, const CFList* oldAeval, CanonicalForm& A, CFList*& leadingCoeffs, int lengthAeval, bool& foundMultiplier)
+{
+        int index= 1;
+        CFListIterator iter, iter2= factors;
+        for (iter= contents; iter.hasItem(); iter++, iter2++, index++)
+        {
+          if (fdivides (iter.getItem(), LCmultiplier))
+          {
+            if ((LCmultiplier/iter.getItem()).inCoeffDomain() &&
+                !isOnlyLeadingCoeff(iter2.getItem())) //content divides LCmultiplier completely and factor consists of more terms than just the leading coeff
+            {
+              Variable xx= Variable (2);
+              CanonicalForm vars;
+              vars= power (xx, degree (LC (getItem(oldBiFactors, index),1),
+                                        xx));
+              for (int i= 0; i < lengthAeval; i++)
+              {
+                if (oldAeval[i].isEmpty())
+                  continue;
+                xx= oldAeval[i].getFirst().mvar();
+                vars *= power (xx, degree (LC (getItem(oldAeval[i], index),1),
+                                           xx));
+              }
+              if (vars.level() <= 2)
+              {
+                int index2= 1;
+                for (CFListIterator iter3= leadingCoeffs[lengthAeval-1];
+                     iter3.hasItem(); iter3++, index2++)
+                {
+                  if (index2 == index)
+                  {
+                    iter3.getItem() /= LCmultiplier;
+                    break;
+                  }
+                }
+                A /= LCmultiplier;
+                foundMultiplier= true;
+                iter.getItem()= 1;
+              }
+            }
+          }
+        }
+}
+
+void
+LCHeuristic5 (const CFList& oldBiFactors, const CFList* oldAeval, const CFList& contents, const CFList& factors, const CanonicalForm& testVars, int lengthAeval, CFList*& leadingCoeffs, CanonicalForm& A, CanonicalForm& LCmultiplier, bool& foundMultiplier)
+{
+          int index=1;
+          CFListIterator iter, iter2= factors;
+          for (iter= contents; iter.hasItem(); iter++, iter2++, index++)
+          {
+            if (!iter.getItem().isOne() &&
+                fdivides (iter.getItem(), LCmultiplier))
+            {
+              if (!isOnlyLeadingCoeff (iter2.getItem())) // factor is more than just leading coeff
+              {
+                int index2= 1;
+                for (iter2= leadingCoeffs[lengthAeval-1]; iter2.hasItem();
+                     iter2++, index2++)
+                {
+                  if (index2 == index)
+                  {
+                    iter2.getItem() /= iter.getItem();
+                    foundMultiplier= true;
+                    break;
+                  }
+                }
+                A /= iter.getItem();
+                LCmultiplier /= iter.getItem();
+                iter.getItem()= 1;
+              }
+              else if (fdivides (getVars (LCmultiplier), testVars))//factor consists of just leading coeff
+              {
+                Variable xx= Variable (2);
+                CanonicalForm vars;
+                vars= power (xx, degree (LC (getItem(oldBiFactors, index),1),
+                                          xx));
+                for (int i= 0; i < lengthAeval; i++)
+                {
+                  if (oldAeval[i].isEmpty())
+                    continue;
+                  xx= oldAeval[i].getFirst().mvar();
+                  vars *= power (xx, degree (LC (getItem(oldAeval[i], index),1),
+                                             xx));
+                }
+                if (myGetVars(content(getItem(leadingCoeffs[lengthAeval-1],index),1))
+                    /myGetVars (LCmultiplier) == vars)
+                {
+                  int index2= 1;
+                  for (iter2= leadingCoeffs[lengthAeval-1]; iter2.hasItem();
+                       iter2++, index2++)
+                  {
+                    if (index2 == index)
+                    {
+                      iter2.getItem() /= LCmultiplier;
+                      foundMultiplier= true;
+                      break;
+                    }
+                  }
+                  A /= LCmultiplier;
+                  iter.getItem()= 1;
+                }
+              }
+            }
+          }
+}
+
 CFList
 extFactorize (const CanonicalForm& F, const ExtensionInfo& info);
@@ -2941,10 +3100,9 @@
                         evaluation);
 
-  CFListIterator iter2, iter;
+  CFListIterator iter;
   CFList bufLeadingCoeffs2= leadingCoeffs2[lengthAeval2-1];
   bufBiFactors= biFactors;
   bufA= A;
-  CanonicalForm bufLCmultiplier= LCmultiplier;
-  CanonicalForm testVars;
+  CanonicalForm testVars, bufLCmultiplier= LCmultiplier;
   if (!LCmultiplierIsConst)
   {
@@ -3022,24 +3180,9 @@
       LCheuristic= true;
       factors= oldFactors;
-      CanonicalForm cont;
       CFList contents, LCs;
-      int index=1;
       bool foundTrueMultiplier= false;
-      for (iter= factors; iter.hasItem(); iter++, index++)
-      {
-        cont= content (iter.getItem(), 1);
-        cont= gcd (cont , LCmultiplier);
-        contents.append (cont);
-        if (cont.inCoeffDomain()) // trivial content->LCmultiplier needs to go there
-        {
-          foundTrueMultiplier= true;
-          int index2= 1;
-          for (iter2= leadingCoeffs2[lengthAeval2-1]; iter2.hasItem(); iter2++,
-                                                                    index2++)
-          {
-            if (index2 == index)
-              continue;
-            iter2.getItem() /= LCmultiplier;
-          }
+      LCHeuristic3 (LCmultiplier, factors, leadingCoeffs2[lengthAeval2-1], contents, LCs, foundTrueMultiplier);
+      if (foundTrueMultiplier)
+      {
           A= oldA;
           leadingCoeffs= leadingCoeffs2[lengthAeval2-1];
@@ -3048,127 +3191,20 @@
           prepareLeadingCoeffs (leadingCoeffs2, A, Aeval, A.level(),
                                 leadingCoeffs, biFactors, evaluation);
-          break;
-        }
-        else
-          LCs.append (LC (iter.getItem()/cont, 1));
-      }
-      if (!foundTrueMultiplier)
-      {
-        index= 1;
-        iter2= factors;
+      }
+      else
+      {
         bool foundMultiplier= false;
-        for (iter= contents; iter.hasItem(); iter++, iter2++, index++)
-        {
-          if (fdivides (iter.getItem(), LCmultiplier))
-          {
-            if ((LCmultiplier/iter.getItem()).inCoeffDomain() &&
-                !isOnlyLeadingCoeff(iter2.getItem())) //content divides LCmultiplier completely and factor consists of more terms than just the leading coeff
-            {
-              Variable xx= Variable (2);
-              CanonicalForm vars;
-              vars= power (xx, degree (LC (getItem(oldBiFactors, index),1),
-                                        xx));
-              for (int i= 0; i < lengthAeval2; i++)
-              {
-                if (oldAeval[i].isEmpty())
-                  continue;
-                xx= oldAeval[i].getFirst().mvar();
-                vars *= power (xx, degree (LC (getItem(oldAeval[i], index),1),
-                                           xx));
-              }
-              if (vars.level() <= 2)
-              {
-                int index2= 1;
-                for (CFListIterator iter3= leadingCoeffs2[lengthAeval2-1];
-                     iter3.hasItem(); iter3++, index2++)
-                {
-                  if (index2 == index)
-                  {
-                    iter3.getItem() /= LCmultiplier;
-                    break;
-                  }
-                }
-                A /= LCmultiplier;
-                foundMultiplier= true;
-                iter.getItem()= 1;
-              }
-            }
-          }
-        }
+        LCHeuristic4 (LCmultiplier, factors, oldBiFactors, contents, oldAeval, A, leadingCoeffs2, lengthAeval2, foundMultiplier);
+
         // coming from above: divide out more LCmultiplier if possible
         if (foundMultiplier)
         {
           foundMultiplier= false;
-          index=1;
-          iter2= factors;
-          for (iter= contents; iter.hasItem(); iter++, iter2++, index++)
-          {
-            if (!iter.getItem().isOne() &&
-                fdivides (iter.getItem(), LCmultiplier))
-            {
-              if (!isOnlyLeadingCoeff (iter2.getItem())) // factor is more than just leading coeff
-              {
-                int index2= 1;
-                for (iter2= leadingCoeffs2[lengthAeval2-1]; iter2.hasItem();
-                     iter2++, index2++)
-                {
-                  if (index2 == index)
-                  {
-                    iter2.getItem() /= iter.getItem();
-                    foundMultiplier= true;
-                    break;
-                  }
-                }
-                A /= iter.getItem();
-                LCmultiplier /= iter.getItem();
-                iter.getItem()= 1;
-              }
-              else if (fdivides (getVars (LCmultiplier), testVars))//factor consists of just leading coeff
-              {
-                //TODO maybe use a sqrffree decomposition of LCmultiplier as below
-                Variable xx= Variable (2);
-                CanonicalForm vars;
-                vars= power (xx, degree (LC (getItem(oldBiFactors, index),1),
-                                          xx));
-                for (int i= 0; i < lengthAeval2; i++)
-                {
-                  if (oldAeval[i].isEmpty())
-                    continue;
-                  xx= oldAeval[i].getFirst().mvar();
-                  vars *= power (xx, degree (LC (getItem(oldAeval[i], index),1),
-                                             xx));
-                }
-                if (myGetVars(content(getItem(leadingCoeffs2[lengthAeval2-1],index),1))
-                    /myGetVars (LCmultiplier) == vars)
-                {
-                  int index2= 1;
-                  for (iter2= leadingCoeffs2[lengthAeval2-1]; iter2.hasItem();
-                       iter2++, index2++)
-                  {
-                    if (index2 == index)
-                    {
-                      iter2.getItem() /= LCmultiplier;
-                      foundMultiplier= true;
-                      break;
-                    }
-                  }
-                  A /= LCmultiplier;
-                  iter.getItem()= 1;
-                }
-              }
-            }
-          }
+          LCHeuristic5 (oldBiFactors, oldAeval, contents, factors, testVars, lengthAeval2, leadingCoeffs2, A, LCmultiplier, foundMultiplier);
         }
         else
         {
-          CanonicalForm pLCs= prod (LCs);
-          if (fdivides (pLCs, LC (oldA,1)) && (LC(oldA,1)/pLCs).inCoeffDomain()) // check if the product of the lead coeffs of the primitive factors equals the lead coeff of the old A
-          {
-            A= oldA;
-            iter2= leadingCoeffs2[lengthAeval2-1];
-            for (iter= contents; iter.hasItem(); iter++, iter2++)
-              iter2.getItem() /= iter.getItem();
-            foundMultiplier= true;
-          }
+          LCHeuristic2 (LCs, contents, A, oldA, leadingCoeffs2[lengthAeval2-1],
+                        foundMultiplier);
           if (!foundMultiplier && fdivides (getVars (LCmultiplier), testVars))
           {
Index: factory/facFqFactorize.h
===================================================================
--- factory/facFqFactorize.h	(revision 4505ca15be27a1d26c6a46880debf164fc3d31b0)
+++ factory/facFqFactorize.h	(revision 3163a2e6bdf16d80875b60e58d864a97a416776a)
@@ -686,6 +686,10 @@
                        );
 
-void changeSecondVariable (CanonicalForm& A, CFList& biFactors, CFList& evaluation, CFList*& oldAeval, int lengthAeval2, const CFList& uniFactors, const Variable& w);
-void distributeLCmultiplier (CanonicalForm& A, CFList& leadingCoeffs, CFList& biFactors, const CFList& evaluation, const CanonicalForm& LCmultipler);
+void
+changeSecondVariable (CanonicalForm& A, CFList& biFactors, CFList& evaluation, CFList*& oldAeval, int lengthAeval2, const CFList& uniFactors, const Variable& w);
+
+void
+distributeLCmultiplier (CanonicalForm& A, CFList& leadingCoeffs, CFList& biFactors, const CFList& evaluation, const CanonicalForm& LCmultipler);
+
 void
 LCHeuristic (CanonicalForm& A, const CanonicalForm& LCmultiplier,
@@ -694,4 +698,19 @@
              const CFList& oldBiFactors);
 
+void
+LCHeuristic2 (const CFList& LCs, const CFList& contents, CanonicalForm& A,
+              const CanonicalForm& oldA, CFList& leadingCoeffs,
+              bool& foundTrueMultiplier);
+
+void
+LCHeuristic3 (const CanonicalForm& LCmultiplier, const CFList& factors, CFList& leadingCoeffs, CFList& contents, CFList& LCs, bool& foundTrueMultiplier);
+
+void
+LCHeuristic4 (const CanonicalForm& LCmultiplier, const CFList& factors, const CFList& oldBiFactors,
+              const CFList& contents, const CFList* oldAeval, CanonicalForm& A, CFList*& leadingCoeffs, int lengthAeval, bool& foundMultiplier);
+
+void
+LCHeuristic5 (const CFList& oldBiFactors, const CFList* oldAeval, const CFList& contents, const CFList& factors, const CanonicalForm& testVars, int lengthAeval, CFList*& leadingCoeffs, CanonicalForm& A, CanonicalForm& LCmultiplier, bool& foundMultiplier);
+
 #endif
 /* FAC_FQ_FACTORIZE_H */
