Index: trunk/ppStack/src/ppStackCleanup.c
===================================================================
--- trunk/ppStack/src/ppStackCleanup.c	(revision 30874)
+++ trunk/ppStack/src/ppStackCleanup.c	(revision 34800)
@@ -22,10 +22,24 @@
     options->outRO = NULL;
 
-    options->expRO->data_exists = false;
-    options->expRO->parent->data_exists = false;
-    options->expRO->parent->parent->data_exists = false;
-    psFree(options->expRO);
-    options->expRO = NULL;
+    if (options->expRO) {
+      options->expRO->data_exists = false;
+      if (options->expRO->parent) {
+	options->expRO->parent->data_exists = false;
+	options->expRO->parent->parent->data_exists = false;
+      }
+      psFree(options->expRO);
+      options->expRO = NULL;
+    }
 
+    if (options->bkgRO) {
+      options->bkgRO->data_exists = false;
+      if (options->bkgRO->parent) {
+	options->bkgRO->parent->data_exists = false;
+	options->bkgRO->parent->parent->data_exists = false;
+      }
+      psFree(options->bkgRO);
+      options->bkgRO = NULL;
+    }
+    
     for (int i = 0; i < options->num; i++) {
         pmCellFreeData(options->cells->data[i]);
@@ -62,7 +76,6 @@
 
 bool ppStackCleanup (pmConfig *config, ppStackOptions *options) {
-
     psExit exitValue = ppStackExitCode(PS_EXIT_SUCCESS); // Exit code
-
+    
     // Ensure everything closes
     if (config) {
@@ -72,4 +85,6 @@
 	ppStackFileActivation(config, PPSTACK_FILES_UNCONV, true);
 	ppStackFileActivation(config, PPSTACK_FILES_PHOT, true);
+	ppStackFileActivation(config, PPSTACK_FILES_MEDIAN_IN, true);
+	ppStackFileActivation(config, PPSTACK_FILES_MEDIAN_OUT, true);
 	if (!ppStackFilesIterateUp(config)) {
 	    psError(psErrorCodeLast(), false, "Unable to close files.");
