Index: /trunk/ppSub/src/Makefile.am
===================================================================
--- /trunk/ppSub/src/Makefile.am	(revision 27108)
+++ /trunk/ppSub/src/Makefile.am	(revision 27109)
@@ -34,4 +34,5 @@
 	ppSubData.c			\
 	ppSubErrorCodes.c		\
+	ppSubExit.c			\
 	ppSubFiles.c			\
 	ppSubLoop.c			\
Index: /trunk/ppSub/src/ppSub.c
===================================================================
--- /trunk/ppSub/src/ppSub.c	(revision 27108)
+++ /trunk/ppSub/src/ppSub.c	(revision 27109)
@@ -63,81 +63,49 @@
 
  die:
-    if (data && data->stats && data->statsFile) {
-        psString stats = psMetadataConfigFormat(data->stats); // Statistics to output
-        if (!stats || strlen(stats) == 0) {
-            psError(PPSUB_ERR_IO, false, "Unable to format statistics file");
-        } else if (fprintf(data->statsFile, "%s", stats) != strlen(stats)) {
-            psError(PPSUB_ERR_IO, true, "Unable to write statistics file");
+    {
+        psExit exitValue = ppSubExitCode(PS_EXIT_SUCCESS); // Exit code
+
+        if (data && data->stats && data->statsFile) {
+            psString stats = psMetadataConfigFormat(data->stats); // Statistics to output
+            if (!stats || strlen(stats) == 0) {
+                psError(PPSUB_ERR_IO, false, "Unable to format statistics file");
+            } else if (fprintf(data->statsFile, "%s", stats) != strlen(stats)) {
+                psError(PPSUB_ERR_IO, true, "Unable to write statistics file");
+            }
+            psFree(stats);
+            if (fclose(data->statsFile) == EOF) {
+                psError(PPSUB_ERR_IO, true, "Unable to close statistics file");
+            }
+            data->statsFile = NULL;
+            pmConfigRunFilenameAddWrite(data->config, "STATS", data->statsName);
+            exitValue = ppSubExitCode(exitValue);
         }
-        psFree(stats);
-        if (fclose(data->statsFile) == EOF) {
-            psError(PPSUB_ERR_IO, true, "Unable to close statistics file");
+
+        if (config && !ppSubFilesIterateUp(config, PPSUB_FILES_ALL)) {
+            psError(psErrorCodeLast(), false, "Unable to close files.");
+            exitValue = ppSubExitCode(exitValue);
         }
-        data->statsFile = NULL;
-        pmConfigRunFilenameAddWrite(data->config, "STATS", data->statsName);
+
+        if (data) {
+            psString dump_file = psMetadataLookupStr(NULL, data->config->arguments, "-dumpconfig");
+            if (dump_file) {
+                if (!pmConfigDump(data->config, dump_file)) {
+                    psError(PPSUB_ERR_IO, false, "Unable to dump configuration.");
+                    exitValue = ppSubExitCode(exitValue);
+                }
+            }
+            psFree(data);
+        }
+
+        psTrace("ppSub", 1, "Finished at %f sec\n", psTimerMark("ppSub"));
+        psTimerStop();
+
+        pmVisualClose(); //close plot windows, if -visual is set
+        pmModelClassCleanup();
+        pmConfigDone();
+        psLibFinalize();
+
+        exitValue = ppSubExitCode(exitValue);
+        exit(exitValue);
     }
-
-    psExit exitValue = PS_EXIT_SUCCESS;        // Exit value for program
-    psErrorCode errorCode = psErrorCodeLast(); // Error code
-    if (errorCode != PS_ERR_NONE) {
-        psErrorStackPrint(stderr, "Unable to perform subtraction.");
-        switch (errorCode) {
-          case PPSUB_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 PPSUB_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 PPSUB_ERR_ARGUMENTS:
-          case PPSUB_ERR_CONFIG:
-            exitValue = PS_EXIT_CONFIG_ERROR;
-            break;
-          case PPSUB_ERR_DATA:
-          case PPSUB_ERR_NO_OVERLAP:
-            exitValue = PS_EXIT_DATA_ERROR;
-            break;
-          case PS_ERR_UNEXPECTED_NULL:
-          case PS_ERR_PROGRAMMING:
-          case PPSUB_ERR_NOT_IMPLEMENTED:
-          case PPSUB_ERR_PROG:
-          default:
-            // It's a programming error if we're not dealing with the error correctly
-            exitValue = PS_EXIT_PROG_ERROR;
-            break;
-        }
-    }
-
-    if (config && !ppSubFilesIterateUp(config, PPSUB_FILES_ALL)) {
-        psErrorStackPrint(stderr, "Unable to close files.");
-    }
-
-    if (data) {
-        psString dump_file = psMetadataLookupStr(NULL, data->config->arguments, "-dumpconfig");
-        if (dump_file) {
-            if (!pmConfigDump(data->config, dump_file)) {
-                psError(PPSUB_ERR_IO, false, "Unable to dump configuration.");
-            }
-        }
-        psFree(data);
-    }
-
-    psTrace("ppSub", 1, "Finished at %f sec\n", psTimerMark("ppSub"));
-    psTimerStop();
-
-    pmVisualClose(); //close plot windows, if -visual is set
-    pmModelClassCleanup();
-    pmConfigDone();
-    psLibFinalize();
-
-    exit(exitValue);
 }
Index: /trunk/ppSub/src/ppSub.h
===================================================================
--- /trunk/ppSub/src/ppSub.h	(revision 27108)
+++ /trunk/ppSub/src/ppSub.h	(revision 27109)
@@ -165,4 +165,8 @@
 bool ppSubCopyPSF (pmFPAfile *output, pmFPAfile *input, pmFPAview *view);
 
+/// Return appropriate exit code
+psExit ppSubExitCode(psExit exitValue   // Current exit value
+    );
+
 ///@}
 #endif
Index: /trunk/ppSub/src/ppSubExit.c
===================================================================
--- /trunk/ppSub/src/ppSubExit.c	(revision 27109)
+++ /trunk/ppSub/src/ppSubExit.c	(revision 27109)
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <pslib.h>
+
+#include "ppSub.h"
+
+psExit ppSubExitCode(psExit exitValue)
+{
+    if (exitValue != PS_EXIT_SUCCESS) {
+        return exitValue;
+    }
+
+    psErrorCode errorCode = psErrorCodeLast(); // Error code
+    if (errorCode != PS_ERR_NONE) {
+        psErrorStackPrint(stderr, "Error in subtraction:");
+        switch (errorCode) {
+          case PPSUB_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 PPSUB_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 PPSUB_ERR_ARGUMENTS:
+          case PPSUB_ERR_CONFIG:
+            exitValue = PS_EXIT_CONFIG_ERROR;
+            break;
+          case PPSUB_ERR_DATA:
+          case PPSUB_ERR_NO_OVERLAP:
+            exitValue = PS_EXIT_DATA_ERROR;
+            break;
+          case PS_ERR_UNEXPECTED_NULL:
+          case PS_ERR_PROGRAMMING:
+          case PPSUB_ERR_NOT_IMPLEMENTED:
+          case PPSUB_ERR_PROG:
+          default:
+            // It's a programming error if we're not dealing with the error correctly
+            exitValue = PS_EXIT_PROG_ERROR;
+            break;
+        }
+    }
+
+    return exitValue;
+}
