                  (conj (pop chunks) l))
                (conj chunks achunk))
              (vector achunk)))))
+
+(defn- finish-bin-index
+  [bin-index]
+  (->> bin-index
+       (seq)
+       (sort-by first)
+       (map (partial zipmap [:bin :chunks]))))
 
 (defn- init-linear-index []
   {:index (gen-vec max-lidx-size 0)

                                  no-coordinate-alns)
            indices' (if new-ref?
                       (assoc indices ref-name {:meta (:meta-data idx-status)
                                                :bins (:bin-index idx-status)
                                                :lidx (optimize-lidx (:index (:linear-index idx-status))
                                                                     (:largest-seen (:linear-index idx-status)))})
                       indices)]
        (recur rest ref-name' idx-status' no-coordinate-alns' indices'))
      (assoc indices ref-name {:meta (:meta-data idx-status)
                               :bins (:bin-index idx-status)
                               :lidx (optimize-lidx (:index (:linear-index idx-status))
                                                    (:largest-seen (:linear-index idx-status)))}
                     :no-coordinate-alns no-coordinate-alns))))

         lidx* []
         last-non-zero 0]
    (if (<= i largest-lidx-seen)
      (let [l (long (nth lidx i))]
        (if (zero? l)
          (recur (inc i) (conj lidx* last-non-zero) last-non-zero)
          (recur (inc i) (conj lidx* l) l)))
