Index: trunk/ppStack/src/ppStack.c
===================================================================
--- trunk/ppStack/src/ppStack.c	(revision 27004)
+++ trunk/ppStack/src/ppStack.c	(revision 27075)
@@ -55,57 +55,68 @@
 
 
-     // Common code for the death.
-die:
-    psTrace("ppStack", 1, "Finished at %f sec\n", psTimerMark(TIMER_NAME));
-    psTimerStop();
+ die:
+    // Common code for the death.
+    {
+        psExit exitValue = PS_EXIT_SUCCESS;        // Exit value for program
+        psErrorCode errorCode = psErrorCodeLast(); // Error code
+        if (errorCode != PS_ERR_NONE) {
+            psErrorStackPrint(stderr, "Unable to perform stack.");
+            switch (errorCode) {
+              case PPSTACK_ERR_UNKNOWN:
+              case PS_ERR_UNKNOWN:
+                exitValue = PS_EXIT_UNKNOWN_ERROR;
+                break;
+              case PS_ERR_IO:
+              case PS_ERR_DB_CLIENT:
+              case PS_ERR_DB_SERVER:
+              case PS_ERR_BAD_FITS:
+              case PS_ERR_OS_CALL_FAILED:
+              case PPSTACK_ERR_IO:
+                exitValue = PS_EXIT_SYS_ERROR;
+                break;
+              case PS_ERR_BAD_PARAMETER_VALUE:
+              case PS_ERR_BAD_PARAMETER_TYPE:
+              case PS_ERR_BAD_PARAMETER_NULL:
+              case PS_ERR_BAD_PARAMETER_SIZE:
+              case PPSTACK_ERR_ARGUMENTS:
+              case PPSTACK_ERR_CONFIG:
+                exitValue = PS_EXIT_CONFIG_ERROR;
+                break;
+              case PPSTACK_ERR_PSF:
+              case PPSTACK_ERR_REJECTED:
+              case PPSTACK_ERR_DATA:
+                exitValue = PS_EXIT_DATA_ERROR;
+                break;
+              case PS_ERR_UNEXPECTED_NULL:
+              case PS_ERR_PROGRAMMING:
+              case PPSTACK_ERR_NOT_IMPLEMENTED:
+              case PPSTACK_ERR_PROG:
+              default:
+                // It's a programming error if we're not dealing with the error correctly
+                exitValue = PS_EXIT_PROG_ERROR;
+                break;
+            }
+        }
 
-    psFree(config);
-    pmModelClassCleanup();
-    pmConfigDone();
-    psLibFinalize();
-    pmVisualClose();
+        // Ensure everything closes
+        ppStackFileActivation(config, PPSTACK_FILES_PREPARE, true);
+        ppStackFileActivation(config, PPSTACK_FILES_CONVOLVE, true);
+        ppStackFileActivation(config, PPSTACK_FILES_COMBINE, true);
+        ppStackFileActivation(config, PPSTACK_FILES_PHOT, true);
+        if (!ppStackFilesIterateUp(config)) {
+            psErrorStackPrint(stderr, "Unable to close files.");
+        }
 
-    psExit exitValue = PS_EXIT_SUCCESS;        // Exit value for program
-    psErrorCode errorCode = psErrorCodeLast(); // Error code
-    if (errorCode != PS_ERR_NONE) {
-        psErrorStackPrint(stderr, "Unable to perform stack.");
-        switch (errorCode) {
-          case PPSTACK_ERR_UNKNOWN:
-          case PS_ERR_UNKNOWN:
-            exitValue = PS_EXIT_UNKNOWN_ERROR;
-            break;
-          case PS_ERR_IO:
-          case PS_ERR_DB_CLIENT:
-          case PS_ERR_DB_SERVER:
-          case PS_ERR_BAD_FITS:
-          case PS_ERR_OS_CALL_FAILED:
-          case PPSTACK_ERR_IO:
-            exitValue = PS_EXIT_SYS_ERROR;
-            break;
-          case PS_ERR_BAD_PARAMETER_VALUE:
-          case PS_ERR_BAD_PARAMETER_TYPE:
-          case PS_ERR_BAD_PARAMETER_NULL:
-          case PS_ERR_BAD_PARAMETER_SIZE:
-          case PPSTACK_ERR_ARGUMENTS:
-          case PPSTACK_ERR_CONFIG:
-            exitValue = PS_EXIT_CONFIG_ERROR;
-            break;
-          case PPSTACK_ERR_PSF:
-          case PPSTACK_ERR_REJECTED:
-          case PPSTACK_ERR_DATA:
-            exitValue = PS_EXIT_DATA_ERROR;
-            break;
-          case PS_ERR_UNEXPECTED_NULL:
-          case PS_ERR_PROGRAMMING:
-          case PPSTACK_ERR_NOT_IMPLEMENTED:
-          case PPSTACK_ERR_PROG:
-          default:
-            // It's a programming error if we're not dealing with the error correctly
-            exitValue = PS_EXIT_PROG_ERROR;
-            break;
-        }
+        psTrace("ppStack", 1, "Finished at %f sec\n", psTimerMark(TIMER_NAME));
+        psTimerStop();
+
+        psFree(config);
+        pmModelClassCleanup();
+        pmConfigDone();
+        psLibFinalize();
+        pmVisualClose();
+
+        exit(exitValue);
     }
-
-    exit(exitValue);
 }
 
