   (find-overlap-intervals* [this start end]
     (find-nclist-overlap-intervals nclist start end)))
 
-(defn- compare-nclist [a b]
-  (let [res (Long/signum (- (:start a) (:start b)))]
-    (if (zero? res)
-      (Long/signum (- (:end b) (:end a)))
-      res)))
 (declare make-nclist*)
 
 (defn- make-nested-intervals [intervals]
-  (when (seq intervals)
-    (let [head (first intervals)
-          [nested-intervals rests]
-          (split-with #(<= (:start  head) (:start %) (:end %) (:end head))
+  (when-first [{:keys [start end] :as head} intervals]
+    (let [[nested-intervals rests]
+          (split-with #(<= start (:start %) (:end %) end)
                       (next intervals))]
-      (cons [(:end head) [head (make-nclist* nested-intervals)]]
+      (cons [end [head (make-nclist* nested-intervals)]]
             (lazy-seq (make-nested-intervals rests))))))
 
 (defn- make-nclist* [intervals]
-  (let [sorted-intervals (sort compare-nclist intervals)]
+  (let [sorted-intervals (sort-by (juxt :start (comp - :end)) intervals)]
     (into (sorted-map) (make-nested-intervals sorted-intervals))))
 
 (defn- make-nclist [intervals]


(defn index-intervals
  "Make indexes for intervals to find overlaps."
  [intervals {:keys [structure] :or {structure :sorted-map}}]
  (->> intervals
       (group-by :chr)
       (into {} (map (fn [[k vs]]
                       [k
                        (case structure
                          :sorted-map (make-sorted-map-intervals vs)
                          :nclist (make-nclist vs))])))))

(defn find-overlap-intervals
  "Find intervals that are on the given `chr` and overlap the given interval

       (into (sorted-map))
       (->SortedMapIntervals)))

(defn- find-nclist-overlap-intervals [nclist start end]
  (when-let [target-intervals
             (->> (subseq nclist  >= start)
                  (map second)
                  (take-while #(<= (:start (first %)) end)))]
    (mapcat #(cons (first %)
                   (find-nclist-overlap-intervals (second %)
                                                  start end))
