Index: Singular/LIB/multigrading.lib
===================================================================
--- Singular/LIB/multigrading.lib	(revision bc4941560856aa3e6fb913d32837d1d18ad1e01e)
+++ Singular/LIB/multigrading.lib	(revision e7c67af3ca54121581499413ed55f282b49e8ac6)
@@ -308,6 +308,4 @@
     }
   }
-  if( !defined(S) )
-  {}
 */
 
@@ -318,5 +316,5 @@
 
   def H = attrib(L, attrGroupHNF);
-  if( !defined(H) || typeof(H) != "intmat")
+  if( typeof(H) != "intmat")
   {
     attrib(L, attrGroupHNF, hermiteNormalForm(L));
@@ -324,5 +322,5 @@
 
   def HH = attrib(L, attrGroupSNF);
-  if( !defined(HH) || typeof(HH) != "intmat")
+  if( typeof(HH) != "intmat")
   {
     attrib(L, attrGroupSNF, smithNormalForm(L));
@@ -470,14 +468,17 @@
   string attrGradingGroup = "gradingGroup";
 
-  if( size(#) > 0 )
-  {
-    if( typeof(#[1]) == "intmat" )
-    {
-      def L = createGroup(M, #[1]);
-    }
-
-    if( isGroup(#[1]) )
-    {
-      def L = #[1];
+  int i = 1;
+  if( size(#) >= i )
+  {
+    def a = #[i];
+    if( typeof(a) == "intmat" )
+    {
+      def L = createGroup(M, a);
+      i++;
+    }
+
+    if( isGroup(a) )
+    {
+      def L = a;
 
       if( !isSublattice(M, L[1]) )
@@ -485,5 +486,8 @@
         ERROR("Multigrading is not contained in the grading group!");
       }
-    }
+      i++;
+    }
+    if( i == 1 ){ ERROR("Wrong arguments: no group given?"); }
+    kill a;
   }
   else
@@ -492,11 +496,10 @@
   }
 
-  if( !defined(L) ){ ERROR("Wrong arguments: no group given?"); }
 
   attrib(basering, attrMgrad, M);
   attrib(basering, attrGradingGroup, L);
 
-  ideal Q = ideal(basering);
-  if( !isHomogeneous(Q) ) // easy now, but would be hard before setting ring attributes!
+  ideal Q = ideal(basering); // quotient ideal is assumed to be a GB!
+  if( !isHomogeneous(Q, "checkGens") ) // easy now, but would be hard before setting ring attributes!
   {
     "Warning: your quotient ideal is not homogenous (multigrading was set anyway)!";
@@ -704,5 +707,5 @@
   }
 
-  if( !defined(R) )
+  if( i == 1 )
   {
     def R = basering;
@@ -805,8 +808,10 @@
   if( size(#) >= i )
   {
-    if( ( typeof(#[i]) == "ring" ) or ( typeof(#[i]) == "qring" ) )
+    def a = #[i];
+    if( ( typeof(a) == "ring" ) or ( typeof(a) == "qring" ) )
     {
       i++;
     }
+    kill a;
   }
 
@@ -820,12 +825,19 @@
 
 
-  def T = G[2];
+  def T = G[2]; 
 
   if( size(#) >= i )
   {
-    if( #[i] == "hermite" )
+    def a = #[i];
+
+    if( typeof(a) != "string" )
+    {
+      ERROR("Sorry wrong arguments!");
+    }
+    
+    if( a == "hermite" )
     {
       def M = attrib(T, attrGradingGroupHNF);
-      if( (!defined(M)) or (typeof(M) != "intmat") )
+      if( typeof(M) != "intmat" )
       {
         if( size(G) > 2 )
@@ -840,8 +852,8 @@
     }
 
-    if( #[i] == "smith" )
+    if( a == "smith" )
     {
       def M = attrib(T, attrGradingGroupSNF);
-      if( (!defined(M)) or (typeof(M) != "intmat") )
+      if( typeof(M) != "intmat" )
       {
         if( size(G) > 2 )
@@ -1736,25 +1748,28 @@
 "
 {
-  if( size(#) > 0 )
-  {
-    if( typeof(#[1]) == "intmat" )
-    {
-      intmat H = hermiteNormalForm(#[1]);
-    } else
-    {
-      if( typeof(#[1]) == "list" )
-      {
-        list L = #[1];
-        intmat H = attrib(L, "hermite"); // todo
-      }
-    }
-
-  }
-  if( !defined(H) )
+  int i = 1;
+  if( size(#) >= i )
+  {
+    def a = #[1];
+    if( typeof(a) == "intmat" )
+    {
+      intmat H = hermiteNormalForm(a);
+      i++;
+    }
+    if( typeof(a) == "list" )
+    {
+      list L = a;
+      intmat H = attrib(L, "hermite"); // todo
+      i++;
+    }
+    kill a;
+  }
+  
+  if( i == 1 )
   {
     intmat H = getLattice("hermite");
   }
 
-  int x, k, i, row;
+  int x, k, row;
 
   int r = nrows(H);
@@ -1840,13 +1855,17 @@
 "
 {
-  if( size(#) > 0 )
-  {
-    if( typeof(#[1]) == "intmat" )
-    {
-      intmat grad = #[1];
-    }
-  }
-
-  if( !defined(grad) )
+  int i = 1;
+  if( size(#) >= i )
+  {
+    def a = #[1];
+    if( typeof(a) == "intmat" )
+    {
+      intmat grad = a;
+      i++;
+    }
+    kill a;
+  }
+
+  if( i == 1 )
   {
     intmat grad = getVariableWeights();
@@ -1854,5 +1873,5 @@
 
   intmat newgg[nrows(grad)][size(f)-1];
-  int i,j;
+  int j;
   intvec l = grad*leadexp(f);
   intvec v;
@@ -1961,5 +1980,5 @@
 
   intmat oldgrad=getVariableWeights(pre);
-  intmat oldtor=getLattice(pre);
+  intmat oldlat=getLattice(pre);
 
   int n=nvars(pre);
@@ -1970,4 +1989,5 @@
   intmat newgrad[pp][np];
 
+  //This will set the finest grading on the image ring. We will proceed by coarsening this grading until f becomes homogeneous. 
   for(i=1;i<=np;i++){ newgrad[p+i,i]=1;}
 
@@ -1976,5 +1996,5 @@
 
 
-  list newtor;
+  list newlat;
   intmat toadd;
   int columns=0;
@@ -1987,10 +2007,11 @@
     for(j=1;j<=n;j++){ toadd1[i,j]=oldgrad[i,j];}
   }
-
+  
+  // This will make the images of homogeneous elements homogeneous, namely the variables of the preimage ring.
   for(i=1;i<=n;i++){
     im=f[i];
     //im;
     toadd = defineHomogeneous(im, newgrad);
-    newtor=insert(newtor,toadd);
+    newlat=insert(newlat,toadd);
     columns=columns+ncols(toadd);
 
@@ -1999,8 +2020,8 @@
   }
 
-  newtor=insert(newtor,toadd1);
+  newlat=insert(newlat,toadd1);
   columns=columns+ncols(toadd1);
 
-
+  //If the image ring is a quotient ring by some ideal, we have to coarsen the grading in order to make the ideal homogeneous.
   if(typeof(basering)=="qring"){
     //"Entering qring";
@@ -2010,28 +2031,29 @@
       //toadd;
       columns=columns+ncols(toadd);
-      newtor=insert(newtor,toadd);
-    }
-  }
-
-  //newtor;
-  intmat imofoldtor[pp][ncols(oldtor)];
-  for(i=1; i<=nrows(oldtor);i++){
-    for(j=1; j<=ncols(oldtor); j++){
-      imofoldtor[i,j]=oldtor[i,j];
-    }
-  }
-
-  columns=columns+ncols(oldtor);
-  newtor=insert(newtor, imofoldtor);
+      newlat=insert(newlat,toadd);
+    }
+  }
+
+  //The grading group of the preimage ring might not have been torsion free. We have to add this torsion to the grading group of the image ring.
+  intmat imofoldlat[pp][ncols(oldlat)];
+  for(i=1; i<=nrows(oldlat);i++){
+    for(j=1; j<=ncols(oldlat); j++){
+      imofoldlat[i,j]=oldlat[i,j];
+    }
+  }
+
+  columns=columns+ncols(oldlat);
+  newlat=insert(newlat, imofoldlat);
 
   intmat gragr[pp][columns];
   columns=0;
-  for(k=1;k<=size(newtor);k++){
+  for(k=1;k<=size(newlat);k++){
     for(i=1;i<=pp;i++){
-      for(j=1;j<=ncols(newtor[k]);j++){gragr[i,j+columns]=newtor[k][i,j];}
-    }
-    columns=columns+ncols(newtor[k]);
-  }
-
+      for(j=1;j<=ncols(newlat[k]);j++){gragr[i,j+columns]=newlat[k][i,j];}
+    }
+    columns=columns+ncols(newlat[k]);
+  }
+  
+  //The following is just for reducing the size of the matrices.
   gragr=hermiteNormalForm(gragr);
   intmat result[pp][pp];
@@ -2389,7 +2411,8 @@
 "
 {
-  if( defined(attrib(A, "grad")) > 0 )
-  {
-    return (attrib(A, "grad"));
+  def a = attrib(A, "grad");
+  if( typeof(a) == "intvec" || typeof(a) == "intmat" )
+  {
+    return (a);
   }
 
@@ -2602,4 +2625,5 @@
     ideal I;
     poly mp, t, tt;
+    intmat V;
   }
   else
@@ -2609,4 +2633,5 @@
       module I;
       vector mp, t, tt;
+      intmat V = getModuleGrading(p);
     }
     else
@@ -2614,13 +2639,4 @@
       ERROR("Wrong ARGUMENT type!");
     }
-  }
-
-  if( typeof(p) == "vector" )
-  {
-    intmat V = getModuleGrading(p);
-  }
-  else
-  {
-    intmat V;
   }
 
@@ -2832,7 +2848,9 @@
 "
 {
+  int @bf = 0;
   if( nameof(basering) != "basering" )
   {
-    def save = basering;
+    @bf = 1;
+    def @save@ = basering;
   }
 
@@ -2849,7 +2867,7 @@
   intmat R = (  matrix2intmat( MM ) ); // transpose
 
-  if( defined(save) > 0 )
-  {
-    setring save;
+  if( @bf == 1 )
+  {
+    setring @save@;
   }
 
