       (chunk/optimize-chunks 0)))
 
 (defn- merge-bins
-  [bins1 bins2]
-  (let [bins->bin-map (fn [bins]
-                        (zipmap (map :bin bins)
-                                (map :chunks bins)))
-        bin-map1 (bins->bin-map bins1)
-        bin-map2 (bins->bin-map bins2)]
-    (-> (merge-with merge-chunks bin-map1 bin-map2)
-        (finish-bin-index))))
+  [bin-map1 bin-map2]
+  (merge-with merge-chunks bin-map1 bin-map2))
 
 (defn- merge-lidx
   [lidx1 lidx2]
-  (concat lidx1 lidx2))
+  {:index (map min (:index lidx1) (:index lidx2))
+   :largest-seen (max (:largest-seen lidx1) (:largest-seen lidx2))})
 
 (defn merge-index
   [& indices]

(defn pos->lidx-offset
  [pos]
  (bit-shift-right (if (<= pos 0) 0 (dec pos)) bam-lidx-shift))

(defn- optimize-lidx
  "Optimizes the linear index for safety. This seems unnecessary, but samtools
  does this process."
  [lidx largest-lidx-seen]
  (loop [i 0
         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)))
      lidx*)))

(defn- init-meta-data []
  {:first-offset  -1


(defn- merge-meta
  [meta1 meta2]
  {:first-offset (let [offsets (filter (partial not= -1) [(:first-offset meta1) (:first-offset meta2)])]
                   (cond
                    (> (count offsets) 1) (apply min offsets)
                    (= (count offsets) 1) (first offsets)
                    :else -1))
   :last-offset (max (:last-offset meta1) (:last-offset meta2))
   :aligned-alns (+ (:aligned-alns meta1) (:aligned-alns meta2))
   :unaligned-alns (+ (:unaligned-alns meta1) (:unaligned-alns meta2))})
