Index: branches/pap/ppStack/src/ppStackMatch.c
===================================================================
--- branches/pap/ppStack/src/ppStackMatch.c	(revision 23948)
+++ branches/pap/ppStack/src/ppStackMatch.c	(revision 25027)
@@ -209,5 +209,5 @@
             pmFPAview *view = pmFPAviewAlloc(0); // View to readout of interest
             view->chip = view->cell = view->readout = 0;
-            psString filename = pmFPAfileNameFromRule(filerule->rule, file, view); // Filename of interest
+            psString filename = pmFPAfileNameFromRule(file->filerule, file, view); // Filename of interest
 
             // Read convolution kernel
@@ -241,12 +241,6 @@
                 !readImage(&readout->variance, options->varianceNames->data[index], config)) {
                 psError(PS_ERR_IO, false, "Unable to read previously produced image.");
-                psFree(imageName);
-                psFree(maskName);
-                psFree(varianceName);
                 return false;
             }
-            psFree(imageName);
-            psFree(maskName);
-            psFree(varianceName);
 
             psRegion *region = psMetadataLookupPtr(NULL, conv->analysis,
@@ -317,4 +311,6 @@
             }
 
+            fake->mask = psImageCopy(NULL, readout->mask, PS_TYPE_IMAGE_MASK);
+
             // Add the background into the target image
             psImage *bgImage = stackBackgroundModel(readout, config); // Image of background
@@ -361,4 +357,7 @@
                     psFree(stampSources);
                     psFree(conv);
+                    if (threads > 0) {
+                        pmSubtractionThreadsFinalize(readout, fake);
+                    }
                     return false;
                 }
@@ -375,4 +374,7 @@
                     psFree(stampSources);
                     psFree(conv);
+                    if (threads > 0) {
+                        pmSubtractionThreadsFinalize(readout, fake);
+                    }
                     return false;
                 }
@@ -510,17 +512,20 @@
         psBinaryOp(readout->variance, readout->variance, "*", psScalarAlloc(PS_SQR(norm), PS_TYPE_F32));
     }
-
+   
     // Ensure the background value is zero
     psStats *bg = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV); // Statistics for background
     psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS); // Random number generator
     if (!psImageBackground(bg, NULL, readout->image, readout->mask, maskVal | maskBad, rng)) {
-        psWarning("Can't measure background for image.");
-        psErrorClear();
+      psWarning("Can't measure background for image.");
+      psErrorClear();
     } else {
-        psLogMsg("ppStack", PS_LOG_INFO, "Correcting convolved image background by %lf (+/- %lf)",
-                 psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN), psStatsGetValue(bg, PS_STAT_ROBUST_STDEV));
-        (void)psBinaryOp(readout->image, readout->image, "-",
-                         psScalarAlloc(psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN), PS_TYPE_F32));
-    }
+      if (!psMetadataLookupBool(NULL, config->arguments, "PPSTACK.SKIP.BG.SUB")) {
+	psLogMsg("ppStack", PS_LOG_INFO, "Correcting convolved image background by %lf (+/- %lf)",
+		 psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN), psStatsGetValue(bg, PS_STAT_ROBUST_STDEV));
+	(void)psBinaryOp(readout->image, readout->image, "-",
+			 psScalarAlloc(psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN), PS_TYPE_F32));
+      }
+    }
+    
 
     // Measure the variance level for the weighting
Index: branches/pap/ppStack/src/ppStackPhotometry.c
===================================================================
--- branches/pap/ppStack/src/ppStackPhotometry.c	(revision 23948)
+++ branches/pap/ppStack/src/ppStackPhotometry.c	(revision 25027)
@@ -94,5 +94,5 @@
         psArray *sources = psMetadataLookupPtr(NULL, photRO->analysis, "PSPHOT.SOURCES"); // Sources
         psMetadataAddS32(options->stats, PS_LIST_TAIL, "NUM_SOURCES", 0,
-                         "Number of sources detected", sources->n);
+                         "Number of sources detected", sources ? sources->n : 0);
         psMetadataAddF32(options->stats, PS_LIST_TAIL, "TIME_PHOT", 0,
                          "Time to do photometry", psTimerMark("PPSTACK_PHOT"));
Index: branches/pap/ppStack/src/ppStackThread.c
===================================================================
--- branches/pap/ppStack/src/ppStackThread.c	(revision 23948)
+++ branches/pap/ppStack/src/ppStackThread.c	(revision 25027)
@@ -187,8 +187,9 @@
 
 
+		int zMax = 0;
                 bool keepReading = false;
-                if (pmReadoutMore(ro, imageFits, 0, rows, config)) {
+                if (pmReadoutMore(ro, imageFits, 0, &zMax, rows, config)) {
                     keepReading = true;
-                    if (!pmReadoutReadChunk(ro, imageFits, 0, rows, overlap, config)) {
+                    if (!pmReadoutReadChunk(ro, imageFits, 0, NULL, rows, overlap, config)) {
                         psError(PS_ERR_IO, false, "Unable to read chunk %d for file PPSTACK.INPUT %d",
                                 numChunk, i);
@@ -198,7 +199,7 @@
                 }
 
-                if (pmReadoutMoreMask(ro, maskFits, 0, rows, config)) {
+                if (pmReadoutMoreMask(ro, maskFits, 0, &zMax, rows, config)) {
                     keepReading = true;
-                    if (!pmReadoutReadChunkMask(ro, maskFits, 0, rows, overlap, config)) {
+                    if (!pmReadoutReadChunkMask(ro, maskFits, 0, NULL, rows, overlap, config)) {
                         psError(PS_ERR_IO, false, "Unable to read chunk %d for file PPSTACK.INPUT.MASK %d",
                                 numChunk, i);
@@ -208,7 +209,7 @@
                 }
 
-                if (pmReadoutMoreVariance(ro, varianceFits, 0, rows, config)) {
+                if (pmReadoutMoreVariance(ro, varianceFits, 0, &zMax, rows, config)) {
                     keepReading = true;
-                    if (!pmReadoutReadChunkVariance(ro, varianceFits, 0, rows, overlap, config)) {
+                    if (!pmReadoutReadChunkVariance(ro, varianceFits, 0, NULL, rows, overlap, config)) {
                         psError(PS_ERR_IO, false,
                                 "Unable to read chunk %d for file PPSTACK.INPUT.VARIANCE %d",
