Index: trunk/ppImage/src/ppImageLoop.c
===================================================================
--- trunk/ppImage/src/ppImageLoop.c	(revision 20444)
+++ trunk/ppImage/src/ppImageLoop.c	(revision 20771)
@@ -95,4 +95,9 @@
                     ESCAPE("Unable to detrend readout");
                 }
+
+		// free detrend images potentially in use: MASK, BIAS, DARK, SHUTTER, FLAT
+		if (!ppImageDetrendFree (config, view)) {
+		    ESCAPE("Unable to free detrend images");
+		}
             }
 
@@ -100,5 +105,13 @@
                 ppImageDetrendRecord(cell, config, options, view);
             }
-        }
+	    // free detrend images potentially in use: MASK, BIAS, DARK, SHUTTER, FLAT
+	    if (!ppImageDetrendFree (config, view)) {
+		ESCAPE("Unable to free detrend images");
+	    }
+        }
+	// free detrend images potentially in use: MASK, BIAS, DARK, SHUTTER, FLAT
+	if (!ppImageDetrendFree (config, view)) {
+	    ESCAPE("Unable to free detrend images");
+	}
 
         // Apply the fringe correction
@@ -108,4 +121,8 @@
             }
         }
+	// free detrend images potentially in use: MASK, BIAS, DARK, SHUTTER, FLAT
+	if (!ppImageFringeFree (config, view)) {
+	    ESCAPE("Unable to free fringe images");
+	}
 
         // measure various statistics for this image
@@ -134,4 +151,16 @@
             }
         }
+
+	// these may be used by ppImageSubtractBackground.
+	// if these are defined as internal files, drop them here
+	status = true;
+	status &= pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL");
+	status &= pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL.STDEV");
+	status &= pmFPAfileDropInternal (config->files, "PSPHOT.BACKGND");
+	if (!status) {
+	    psError(PSPHOT_ERR_PROG, false, "trouble dropping internal files");
+	    psFree (view);
+	    return false;
+	}
 
         // binning (used for display) must take place after the background is replaced, if desired
