Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 7469)
+++ /trunk/ippTools/src/dettool.c	(revision 7470)
@@ -1785,5 +1785,5 @@
 
 
-static bool rerunMode(pxConfig *config)
+static bool rerunMode1(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -1808,27 +1808,18 @@
         return false;
     }
-    psMetadata *where = psMetadataAlloc();
+    psList *exp_id_list = item->data.list;
 
     // make sure that -exp_id was parsed correctly
     // XXX this can be removed someday
     if (item->type == PS_DATA_METADATA_MULTI) {
-        psListIterator *iter = psListIteratorAlloc(item->data.list, 0, false);
+        psListIterator *iter = psListIteratorAlloc(exp_id_list, 0, false);
         psMetadataItem *mItem = NULL;
         while ((mItem = psListGetAndIncrement(iter))) {
             psString exp_id = mItem->data.V;
             // if exp_id is NULL then it means that -exp_id has not been
-            // specified
+            // specified or something really bad happened
             if (!exp_id) {
                 psError(PS_ERR_UNKNOWN, true,
                         "at least one -exp_id is required");
-                psFree(where);
-                return false;
-            }
-
-            if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id",
-                        PS_META_DUPLICATE_OK, "==", exp_id)) {
-                psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
-                psFree(iter);
-                psFree(where);
                 return false;
             }
@@ -1840,13 +1831,9 @@
     }
 
-    if (where->list->n < 1) {
-        psFree(where);
-        where = NULL;
-    }
-
     // check that the specified exp_ids actually exist in the iteration zero
     // detInputExp set
 
     // add the det_id & iteration == 0 to the where claus
+    psMetadata *where = psMetadataAlloc();
     if (!psMetadataAddS32(where, PS_LIST_TAIL, "det_id", 0, "==",
                 (psS32)atol(det_id))) {
@@ -1868,4 +1855,30 @@
     }
 
-    return true;
-}
+    // build a hash for the valid exp_ids
+    psHash *valid_exp_ids = psHashAlloc(psArrayLength(detrendExps));
+    for (long i = 0; i < psArrayLength(detrendExps); i++) {
+        psHashAdd(valid_exp_ids,
+            ((detInputExpRow *)detrendExps->data[i])->exp_id,
+            ((detInputExpRow *)detrendExps->data[i])->exp_id
+        );
+    }
+
+    psListIterator *iter = psListIteratorAlloc(exp_id_list, 0, false);
+    psMetadataItem *mItem = NULL;
+    while ((mItem = psListGetAndIncrement(iter))) {
+        if (!psHashLookup(valid_exp_ids, (char *)mItem->data.V)) {
+            // invalid exp_id
+            psError(PS_ERR_UNKNOWN, false, "exp_id %s is invalid for det_id %s",
+                    (char *)mItem->data.V, det_id);
+            psFree(valid_exp_ids);
+            psFree(iter);
+            return false;
+        }
+    }
+    psFree(valid_exp_ids);
+    psFree(iter);
+
+    psFree(exp_id_list);
+
+    return true;
+}
