   (println "done")
   pipeline-stages)
 
+(defn- sync-stages [go-url buildviz-url sync-start-time sync-jobs-for-pipelines selected-pipeline-group-names]
+  (->> (goapi/get-stages go-url)
+       (select-stages selected-pipeline-group-names)
+       (emit-start go-url buildviz-url sync-start-time)
+       (mapcat #(stage-instances-from go-url sync-start-time %))
+       (sort-by :scheduled-time)
+       (take-while :finished)
+       (emit-sync-start)
+       (progress/init "Syncing")
+       (map #(add-inputs-for-stage-instance go-url %))
+       (map #(add-job-instances-for-stage-instance go-url %))
+       (map #(aggregate-jobs-for-stage-instance % sync-jobs-for-pipelines))
+       (mapcat stage-instances->builds)
+       (map #(put-to-buildviz buildviz-url %))
+       (map progress/tick)
+       dorun
+       (progress/done)))
+
 (defn -main [& c-args]
-  (def args (parse-opts c-args cli-options))
-
-  (when (or (:help (:options args))
-            (empty? (:arguments args)))
-    (println (usage (:summary args)))
-    (System/exit 0))
-
-  (def go-url (first (:arguments args)))
-  (def buildviz-url (:buildviz-url (:options args)))
-
-  (let [sync-start-time (get-start-date (:sync-start-time (:options args)))
-        sync-jobs-for-pipelines (set (:sync-jobs-for-pipelines (:options args)))
-        selected-pipeline-group-names (set (:pipeline-groups (:options args)))]
-
-    (->> (goapi/get-stages go-url)
-         (select-stages selected-pipeline-group-names)
-         (emit-start sync-start-time)
-         (mapcat #(stage-instances-from sync-start-time %))
-         (sort-by :scheduled-time)
-         (take-while :finished)
-         (emit-sync-start)
-         (progress/init "Syncing")
-         (map add-inputs-for-stage-instance)
-         (map add-job-instances-for-stage-instance)
-         (map #(aggregate-jobs-for-stage-instance % sync-jobs-for-pipelines))
-         (mapcat stage-instances->builds)
-         (map put-to-buildviz)
-         (map progress/tick)
-         dorun
-         (progress/done))))
+  (let [args (parse-opts c-args cli-options)]
+    (when (or (:help (:options args))
+              (empty? (:arguments args)))
+      (println (usage (:summary args)))
+      (System/exit 0))
+
+    (let [go-url (first (:arguments args))
+          buildviz-url (:buildviz-url (:options args))
+          sync-start-time (get-start-date buildviz-url (:sync-start-time (:options args)))
+          sync-jobs-for-pipelines (set (:sync-jobs-for-pipelines (:options args)))
+          selected-pipeline-group-names (set (:pipeline-groups (:options args)))]
+
+      (sync-stages go-url buildviz-url sync-start-time sync-jobs-for-pipelines selected-pipeline-group-names))))

   :scheduled-time (tc/from-long (apply min (map :scheduled_date jobs)))
   :job-names (map :name jobs)})

(defn stage-instances-from [sync-start-time {stage-name :stage pipeline-name :pipeline}]
  (let [safe-build-start-date (t/minus sync-start-time (t/millis 1))]
    (->> (goapi/get-stage-history go-url pipeline-name stage-name)
         (map parse-stage-instance)

      (goaggregate/aggregate-jobs-for-stage stage-instance))))


(defn- build-for-job [go-url stage-instance job-name]
  (let [job-instance (assoc stage-instance :job-name job-name)]
    (-> (goapi/build-for go-url job-instance)
        (assoc :name job-name)
        (assoc :junit-xml (goapi/get-junit-xml go-url job-instance)))))

(defn- add-job-instances-for-stage-instance [go-url stage-instance]
  (let [job-names (:job-names stage-instance)]
    (assoc stage-instance
           :job-instances (map #(build-for-job go-url stage-instance %) job-names))))


(defn parse-stage-instance [{pipeline-run :pipeline_counter
