                       (map before-save-item)
                       (map #(create-transaction-item* s %))))
           (if (delay-balances? (:entity-id validated))
-            (swap! ambient-settings
-                   update-in
-                   [(:entity-id validated) :delayed-account-ids]
-                   concat
-                   account-ids)
+            (swap! ambient-settings #(save-delayed-info % validated))
             (doseq [account-id account-ids]
               (recalculate-account s account-id (:transaction-date validated))))
           (reload s created))))))

     (let [result# (do ~@body)]

       ; Recalculate balances for affected accounts
       ; TODO rework to use recalculate-account
       #_(->> (get-in @ambient-settings [~entity-id :delayed-account-ids])
            (map #(hash-map :account-id % :index -1 :balance 0M))
            (map #(recalculate-account-items ~storage-spec %))
            doall)

       ; clean up the ambient settings as if we were never here
       (swap! ambient-settings dissoc ~entity-id)

  (->> (:items transaction)
       (map :account-id)
       (into #{})))

(defn- save-delayed-info
  [settings {:keys [entity-id transaction-date] :as transaction}]
  (-> settings
      (update-in [entity-id :delayed-account-ids]
                 concat
                 (extract-account-ids transaction))
      (update-in [entity-id :earliest-date] #(if %
                                               (earlier % transaction-date)
                                               transaction-date))))

(defn create
  "Creates a new transaction"
