                      :defs {}
                      :refs {}}]
       (let [[val port] (async/alts! (into ports (select-jobs (:jobs state))))]
-        (if-let [type (:type val)]
-          (if (= parse-ch port)
-            (if (= :study type)
-              (recur [] (handle-study state val))
-              (recur (constrain-jobs state) ((handler type) state val)))
-            (recur (constrain-jobs state) ((job-handler type) state port val)))
+        (if (instance? Throwable val)
+          val
+          (if-let [type (:type val)]
+            (if (= parse-ch port)
+              (if (= :study type)
+                (recur [] (handle-study state val))
+                (recur (constrain-jobs state) ((handler type) state val)))
+              (recur (constrain-jobs state) ((job-handler type) state port val)))
 
-          (if (= 0 (:count (:jobs state)))
-            (log/info "Finished!")
-            (recur [] state)))))))
-
+            (if (= 0 (:count (:jobs state)))
+              (if (empty? (:refs state))
+                (log/info "Finished!")
+                (throw (ex-info (str "Unresolved refs: " (:refs state)) {:refs (:refs state)})))
+              (recur [] state))))))))

    :item-ref handle-item-ref-job
    :item-def handle-item-def-job))

(defn import! [service-document n parse-ch]
  (let [constrain-jobs
        (fn [state]
          (if (< (:count (:jobs state)) n) [parse-ch] []))]

  (update state :jobs #(remove-job % :item-group port)))

(defn handle-item-group-def-job [state port val]
  (log/trace "handle-item-group-def-job:" (:id (:data val)))
  (let [create-ref (map #(create-item-group-ref-job (get-in state [:defs (:study-id val)]) %))
        ref-vec [:refs (:study-id val) :item-group-refs (:id (:data val))]
        ref-jobs (eduction create-ref (get-in state ref-vec))]
    (-> (update state :jobs #(-> (remove-job % :item-group port)
                                 (add-jobs :item-group ref-jobs)))
        (update :defs #(collect-item-group-def % val))
        (dissoc-in ref-vec))))

(defn handle-item-ref-job [state port _]
  (update state :jobs #(remove-job % :item port)))

(defn handle-item-def-job [state port val]
  (let [create-ref (map (fn [ref] (create-item-ref-job (get-in state [:defs (:study-id val)]) ref)))
        ref-vec [:refs (:study-id val) :item-refs (:id (:data val))]
        ref-jobs (eduction create-ref (get-in state ref-vec))]
    (-> (update state :jobs #(-> (remove-job % :item port)
                                 (add-jobs :item ref-jobs)))
        (dissoc-in ref-vec))))

(defn- job-handler [type]
  (case type
