Index: trunk/psModules/src/detrend/pmFlatNormalize.c
===================================================================
--- trunk/psModules/src/detrend/pmFlatNormalize.c	(revision 7278)
+++ trunk/psModules/src/detrend/pmFlatNormalize.c	(revision 7860)
@@ -38,14 +38,9 @@
     psVectorInit(sourceMask, 0);
     for (int i = 0; i < numChips; i++) {
+        // Note: the input gains are in e/ADU; we want to work with ADU/e (bg [ADU] = g [ADU/e] * f [e])
         if (isfinite(chipGains->data.F32[i]) && chipGains->data.F32[i] > 0) {
-            chipGains->data.F32[i] = log(chipGains->data.F32[i]);
+            chipGains->data.F32[i] = -log(chipGains->data.F32[i]);
         } else {
             chipGains->data.F32[i] = 0.0; // Take a wild guess
-            #if 0
-            // Blank out this chip
-            gainMask->data.U8[i] = 1;
-            chipGains->data.F32[i] = NAN;
-            #endif
-
         }
 
@@ -61,4 +56,6 @@
     }
 
+    // Not really sure that we need to iterate, but here we go anyway...
+
     float diff = INFINITY;             // Difference from previous iteration
     psVector *sourceFlux = psVectorAlloc(numSources, PS_TYPE_F32); // The flux in each integration
@@ -70,7 +67,5 @@
         diff = 0.0;
 
-
         // Improve on the fluxes
-        float sumFlux = 0.0;           // Total fluxes
         long numFluxes = 0;             // Number of fluxes
         for (int i = 0; i < numSources; i++) {
@@ -94,16 +89,11 @@
                 sourceFlux->data.F32[i] = NAN;
             }
-            sumFlux += exp(sourceFlux->data.F32[i]);
-            psTrace(__func__, 7, "Flux for exposure %d is %f\n", i, exp(sourceFlux->data.F32[i]));
+            psTrace(__func__, 7, "Flux for exposure %d is %lf\n", i, exp(sourceFlux->data.F32[i]));
         }
-        // Normalise the mean to unity
-        sumFlux /= (float)numFluxes;
-        sumFlux = log(sumFlux);
+
         for (int i = 0; i < numSources; i++) {
-            if (sourceMask->data.U8[i]) {
-                continue;
+            if (!sourceMask->data.U8[i]) {
+                diff += abs((sourceFlux->data.F32[i] - oldSourceFlux->data.F32[i]) / sourceFlux->data.F32[i]);
             }
-            sourceFlux->data.F32[i] -= sumFlux;
-            diff += abs((sourceFlux->data.F32[i] - oldSourceFlux->data.F32[i]) / sourceFlux->data.F32[i]);
         }
 
@@ -127,8 +117,8 @@
                 chipGains->data.F32[i] = NAN;
             }
-            psTrace(__func__, 7, "Gain for chip %d is %f\n", i, exp(chipGains->data.F32[i]));
+            psTrace(__func__, 7, "Gain for chip %d is %lf\n", i, exp(-chipGains->data.F32[i]));
         }
 
-        psTrace(__func__, 2, "Iteration %d: difference is %f\n", iter, diff);
+        psTrace(__func__, 2, "Iteration %d: difference is %e\n", iter, diff);
 
         // Switch the old and new
