        before-validation
        (validation/validate spec rules)))
 
+(defn- item-refs->date-range
+  [item-refs]
+  (when (seq item-refs)
+    ((juxt first last) (->> item-refs
+                            (map second)
+                            sort))))
+
 (defn- after-save
-  [storage {item-refs :item-refs :as reconciliation}]
+  [storage {:keys [id item-refs] :as reconciliation}]
   ; Set reconciled flag on specified transaction items
-  (when (and item-refs (seq item-refs))
-    (doseq [[item-ref transaction-date] item-refs]
-      (set-transaction-item-reconciled storage
-                                       (:id reconciliation)
-                                       item-ref
-                                       transaction-date)))
+  (let [date-range (item-refs->date-range item-refs)]
+    (when date-range
+      (unreconcile-transaction-items-by-reconciliation-id
+        storage
+        id
+        date-range))
+    (when (and item-refs (seq item-refs))
+      (doseq [[item-ref transaction-date] item-refs]
+        (set-transaction-item-reconciled storage
+                                         id
+                                         item-ref
+                                         transaction-date))))
   reconciliation)
 
 (defn- create*

  (let [existing (find-by-id storage (Integer. (:id reconciliation)))]
    (assoc reconciliation :account-id (:account-id existing))))

(defn- item-refs->date-range
  [item-refs]
  (when (seq item-refs)
    ((juxt first last) (->> item-refs
                            (map second)
                            sort))))

(defn update
  "Updates the specified reconciliation"
  [storage-spec reconciliation]
  (with-storage [s storage-spec]
    (let [validated (->> reconciliation
                         (set-account-id s)
                         (validate ::existing-reconciliation (validation-rules s)))]
      (if (validation/valid? validated)
        (let [date-range (item-refs->date-range (:item-ds validated))]
          (->> validated
               before-save
               (update-reconciliation s))
          (when date-range
            (unreconcile-transaction-items-by-reconciliation-id s (:id validated) date-range))
          (when (and (:item-refs validated) (seq (:item-refs validated))) ; TODO: remove this redundancy wth create
            (doseq [[item-ref date] (:item-refs validated)]
              (set-transaction-item-reconciled s (:id validated) item-ref date)))
          (reload s validated))
        validated))))

(defn delete
  "Removes the specified reconciliation from the system. (Only the most recent may be deleted.)"

  ([reconciliation]
   (before-save nil reconciliation))
  ([_ reconciliation]
   (update-in reconciliation [:status] name)))

(defn- append-transaction-item-refs
  [reconciliation storage]
