         ;; bins
         (if (zero? (count (:bins index)))
           (lsb/write-int wtr 0)
-          (let [bins (filter #(not (nil? %)) (:bins index))]
+          (let [bins (:bins index)]
             ;; # of bins
             (lsb/write-int wtr (inc (count bins)))
             (doseq [bin bins]
-              (write-bin wtr bin))
+              (write-bin wtr {:idx (first bin), :chunks (second bin)}))
             ;; meta data
             (write-meta-data wtr (:meta index))))
         ;; linear index

(ns cljam.bam-index.writer
  (:require [clojure.java.io :refer [file]]
            [clojure.tools.logging :as logging]
            [clojure.pprint :refer [pprint]] ;; TODO for debug only
            [cljam.lsb :as lsb]
            [cljam.cigar :as cgr]
            [cljam.util :refer [gen-vec]]

                                :last-offset 0
                                :aligned-alns 0
                                :unaligned-alns 0}
                         :bins {}
                         :lidx []}) refs))
   (make-index! refs alns)))

