Index: trunk/ppSim/src/ppSimLoop.c
===================================================================
--- trunk/ppSim/src/ppSimLoop.c	(revision 14668)
+++ trunk/ppSim/src/ppSimLoop.c	(revision 14815)
@@ -19,10 +19,10 @@
     psArray *stars = psArrayAllocEmpty (1);
     if (type == PPSIM_TYPE_OBJECT) {
-	ppSimLoadStars (stars, fpa, config);
+        ppSimLoadStars (stars, fpa, config);
     }
 
     // Add random stars
     if (type == PPSIM_TYPE_OBJECT) {
-	ppSimMakeStars (stars, fpa, config, rng);
+        ppSimMakeStars (stars, fpa, config, rng);
     }
 
@@ -30,5 +30,5 @@
     psArray *galaxies = psArrayAllocEmpty (1);
     if (type == PPSIM_TYPE_OBJECT) {
-	ppSimMakeGalaxies (galaxies, fpa, config, rng);
+        ppSimMakeGalaxies (galaxies, fpa, config, rng);
     }
 
@@ -40,7 +40,7 @@
     // load any needed files (eg, input image, PSF)
     if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
-	psError(PS_ERR_UNKNOWN, false, "failed IO for fpa in ppSim\n");
-	psFree(view);
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "failed IO for fpa in ppSim\n");
+        psFree(view);
+        return false;
     }
 
@@ -50,23 +50,23 @@
     while ((chip = pmFPAviewNextChip(view, fpa, 1))) {
 
-	// load any needed files (eg, input image, PSF)
-	if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
+        // load any needed files (eg, input image, PSF)
+        if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
             psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d in ppSim\n", view->chip);
-	    psFree (view);
-	    return false;
-	}
+            psFree (view);
+            return false;
+        }
 
-	if (type == PPSIM_TYPE_OBJECT) {
-	    if (!ppSimSetPSF (chip, config)) {
-		psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d in ppSim\n", view->chip);
-		psFree (view);
-		return false;
-	    }
-	}
+        if (type == PPSIM_TYPE_OBJECT) {
+            if (!ppSimSetPSF (chip, config)) {
+                psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d in ppSim\n", view->chip);
+                psFree (view);
+                return false;
+            }
+        }
 
         pmCell *cell;                   // Cell from chip
         while ((cell = pmFPAviewNextCell(view, fpa, 1))) {
 
-	    // check that we are able to work with this cell (readdir must be 1)
+            // check that we are able to work with this cell (readdir must be 1)
             int readdir = psMetadataLookupS32(NULL, cell->concepts, "CELL.READDIR"); // Read direction
             if (readdir != 1) {
@@ -76,37 +76,39 @@
             }
 
-	    // if no readout exists, generate a single readout (upgrade to allow multiple
-	    // readouts?)
-	    if (cell->readouts->n == 0) {
-		// Size, position and orientation of cell
-		int numCols = psMetadataLookupS32(NULL, cell->concepts, "CELL.XSIZE") / binning;
-		int numRows = psMetadataLookupS32(NULL, cell->concepts, "CELL.YSIZE") / binning;
+            // if no readout exists, generate a single readout (upgrade to allow multiple
+            // readouts?)
+            if (cell->readouts->n == 0) {
+                // Size, position and orientation of cell
+                int numCols = psMetadataLookupS32(NULL, cell->concepts, "CELL.XSIZE") / binning;
+                int numRows = psMetadataLookupS32(NULL, cell->concepts, "CELL.YSIZE") / binning;
 
-		// generate a new readout for this cell
+                // generate a new readout for this cell
                 pmReadout *readout = pmReadoutAlloc(cell); // Readout within cell
 
-		// TO DO: Decide if cell is to be windowed, reduce numCols, numRows appropriately
+                // TO DO: Decide if cell is to be windowed, reduce numCols, numRows appropriately
                 readout->image = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Signal in pixels
                 readout->weight = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Noise in pixels
 
-		psImageInit (readout->image, 0.0);
-		psImageInit (readout->weight, 0.0);
-	    }
+                psImageInit (readout->image, 0.0);
+                psImageInit (readout->weight, 0.0);
 
-	    psVector *biasCols = ppSimMakeBiassec (cell, config);
+                psFree(readout);        // Drop reference
+            }
+
+            psVector *biasCols = ppSimMakeBiassec (cell, config);
 
             for (int i = 0; i < cell->readouts->n; i++) {
 
-		pmReadout *readout = cell->readouts->data[i];
-		assert (readout);
+                pmReadout *readout = cell->readouts->data[i];
+                assert (readout);
 
-		// if we have not read in a weight or generated a fake image above, we need to
-		// build one here
-		if (!readout->weight) {
-		    if (!pmReadoutGenerateWeight(readout, true)) {
-			psError (PS_ERR_UNKNOWN, false, "trouble creating weight");
-			return false;
-		    }
-		}
+                // if we have not read in a weight or generated a fake image above, we need to
+                // build one here
+                if (!readout->weight) {
+                    if (!pmReadoutGenerateWeight(readout, true)) {
+                        psError (PS_ERR_UNKNOWN, false, "trouble creating weight");
+                        return false;
+                    }
+                }
 
                 psImage *expCorr = NULL; // Exposure correction per pixel, for adding objects
@@ -115,26 +117,28 @@
                 }
 
-		psVector *biasRows = ppSimMakeBias (readout, config, rng);
-		if (type == PPSIM_TYPE_BIAS) goto done;
-	
-		ppSimMakeDark (readout, config);
-		if (type == PPSIM_TYPE_DARK) goto done;
+                psVector *biasRows = ppSimMakeBias (readout, config, rng);
+                if (type == PPSIM_TYPE_BIAS) goto done;
 
-		ppSimMakeSky (readout, expCorr, type, config);
-		if (type == PPSIM_TYPE_FLAT) goto done;
+                ppSimMakeDark (readout, config);
+                if (type == PPSIM_TYPE_DARK) goto done;
 
-		if (type == PPSIM_TYPE_OBJECT) {
-		    ppSimInsertStars (readout, expCorr, stars, config);
-		}
+                ppSimMakeSky (readout, expCorr, type, config);
+                if (type == PPSIM_TYPE_FLAT) goto done;
 
-		if (type == PPSIM_TYPE_OBJECT) {
-		    ppSimInsertGalaxies (readout, expCorr, galaxies, config);
-		}
+                if (type == PPSIM_TYPE_OBJECT) {
+                    ppSimInsertStars (readout, expCorr, stars, config);
+                }
 
-	    done:
-		ppSimAddNoise(readout->image, readout->weight, cell, config, rng);
+                if (type == PPSIM_TYPE_OBJECT) {
+                    ppSimInsertGalaxies (readout, expCorr, galaxies, config);
+                }
+
+                psFree(expCorr);
+
+            done:
+                ppSimAddNoise(readout->image, readout->weight, cell, config, rng);
                 ppSimSaturate(readout, config);
 
-		ppSimAddOverscan (readout, config, biasCols, biasRows, rng);
+                ppSimAddOverscan (readout, config, biasCols, biasRows, rng);
                 psFree(biasRows);
 
@@ -145,5 +149,5 @@
             psFree(biasCols);
 
-	    ppSimUpdateConceptsCell (cell, config);
+            ppSimUpdateConceptsCell (cell, config);
 
             if (cell->hdu) {
@@ -174,4 +178,7 @@
     }
 
+    psFree(stars);
+    psFree(galaxies);
+
     if (fpa->hdu) {
         ppSimInitHeader(config, fpa, NULL, NULL);
