               0))
          z-ref))))
 
-(defn store-new-z-reference []
+(defn store-new-z-reference [msp]
   (let [z-drive (@state :default-z-drive)]
     (when (and (or (not (core isContinuousFocusEnabled))
                    (core isContinuousFocusDrive z-drive))
-               (not (z-in-msp z-drive)))
+               (not (z-in-msp msp z-drive)))
       (state-assoc! :reference-z (get-z-stage-position z-drive)))))
 
 ;; startup and shutdown

    (start-property-sequences (:properties trigger-sequence))
    (when absolute-slices
      (start-slice-sequence))
    (swap! state assoc :burst-init-time (elapsed-time @state))
    (core startSequenceAcquisition (if (first-trigger-missing?) (inc length) length) 0 true)
    (swap! state assoc-in [:last-stage-positions (core getFocusDevice)]
           (last absolute-slices))))

(defn jvm-time-ms []
  (quot (System/nanoTime) 1000000))

(defn elapsed-time [state]
  (if (state :start-time) (- (jvm-time-ms) (state :start-time)) 0))

(defn core-time-from-tags [tags]
  (try (Double/parseDouble (tags "ElapsedTime-ms")) (catch Exception e nil)))

(defn burst-time [tags state]
  (when (and (:burst-time-offset state) (get tags "ElapsedTime-ms"))
    (+ (core-time-from-tags tags)
       (:burst-time-offset state))))

;; image metadata

