   (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 [(:study-id val) :item-group-refs (:id (:data val))]
-        ref-jobs (eduction create-ref (get-in state [:refs ref-vec]))]
+        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))
-        (update :refs #(dissoc-in % ref-vec)))))
+        (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 #(create-item-ref-job (get-in state [:defs (:study-id val)]) %))
-        ref-vec [(:study-id val) :item-refs (:id (:data val))]
-        ref-jobs (eduction create-ref (get-in state [:refs ref-vec]))]
+  (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)))
-        (update :refs #(dissoc-in % ref-vec)))))
+        (dissoc-in ref-vec))))
 
 (defn- job-handler [type]
   (case type

    :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 :count dec)))

(defn- create-item-group-ref-job [defs ref]
  (log/trace "create-item-group-ref-job:" ref)
  (let [form-def (get-in defs [:form-defs (:form-def-id ref)])]
    (assert form-def)
    (->> (async/chan 1 (assoc-xf :type :item-group-ref))
         (async/pipe (api/create-item-group-ref! form-def ref)))))

(defn- create-item-ref-job [defs ref]
  (log/trace "create-item-ref-job:" ref)
  (let [item-group-def (get-in defs [:item-group-defs (:item-group-def-id ref)])]
    (assert item-group-def)
    (->> (async/chan 1 (assoc-xf :type :item-ref))
         (async/pipe (api/create-item-ref! item-group-def ref)))))

