--- a/src/agf_precondition.cc
+++ b/src/agf_precondition.cc
@@ -157,13 +157,19 @@
   ave=new real_a[nvar];
   if (opt_args.normflag) {
     calc_norm(train, nvar2, ntrain, ave, std);
-    for (dim_ta j=0; j<nvar2; j++) 
-		for (nel_ta i=0; i<ntrain; i++) train[i][j]/=std[j];
+    for (nel_ta i=0; i<ntrain; i++) {
+      for (dim_ta j=0; j<nvar2; j++) {
+        train[i][j]=(train[i][j]-ave[j])/std[j];
+      }
+    }
     //if (argc>=2) print_stats(diagfs, ave, std, nvar2);
     print_stats(diagfs, ave, std, nvar2);
   } else {
     //(kind of a stupid way of doing it... oh well)
-    for (dim_ta i=0; i<nvar2; i++) std[i]=1;
+    for (dim_ta i=0; i<nvar2; i++) {
+      std[i]=1;
+      ave[i]=0;
+    }
   }
 
 
@@ -171,6 +177,17 @@
     gsl_matrix *u;
     gsl_vector *work;
     nel_ta k;
+
+    //always remove averages (since they are wasted...)
+    if (opt_args.normflag==0) {
+      real_a dum[nvar2];
+      calc_norm(train, nvar2, ntrain, ave, dum);
+      for (nel_ta i=0; i<ntrain; i++) {
+        for (dim_ta j=0; j<nvar2; j++) {
+          train[i][j]=train[i][j]-ave[j];
+        }
+      }
+    }
 
     if (ntrain>nvar2) {
       u=gsl_matrix_alloc(ntrain, nvar2);
@@ -261,11 +278,12 @@
       nvar3=nvar;
     }
   } else {
-    mat=zero_matrix<real_a, nel_ta>(nvar, nvar3);
+    mat=zero_matrix<real_a, nel_ta>(nvar, nvar3+1);
     for (dim_ta i=0; i<nvar2; i++) {
       for (dim_ta j=0; j<nvar3; j++) {
-        mat[ind[i]][j]=gsl_matrix_get(v, i, j)/std[j];
-      }
+        mat[ind[i]][j]=gsl_matrix_get(v, i, j)/std[i];
+      }
+      mat[ind[i]][nvar3]=ave[i];	//store averages to right of matrix
     }
   }
 
@@ -299,6 +317,7 @@
       fprintf(stderr, "Unable to open file for writing: %s\n", opt_args.normfile);
       return UNABLE_TO_OPEN_FILE_FOR_WRITING;
     }
+    nvar3++;
     fwrite(&nvar3, sizeof(nvar3), 1, fs);
     fwrite(mat[0], sizeof(real_a), nvar3*nvar, fs);
     fclose(fs);