 
   (valAt [this k not-found]
     (let [idx (loop [ks     (keys m)
+                     n      (count ks)
                      lastki nil]
                 #_(println "LOOP1" lastki ks)
                 (if (not-empty ks)
-                  (let [n   (count ks)
-                        i   (int (/ n 2))
+                  (let [i   (int (/ n 2))
                         ki  (get (vec ks) i)
                         cmp (compare ki k)]
                     #_(println "LOOP2" n i ki cmp)
                     (cond
-                      (> cmp 0) (recur (take i ks)       (if (= kind :find-floor)   lastki ki))  ;; traverse left
-                      (< cmp 0) (recur (drop (inc i) ks) (if (= kind :find-ceiling) lastki ki))  ;; traverse right
+                      (> cmp 0) (recur (take i ks)       i         (leftfn lastki ki))   ;; traverse left
+                      (< cmp 0) (recur (drop (inc i) ks) (- n i 1) (rightfn lastki ki))  ;; traverse right
                       (= cmp 0) ki))
                   lastki))]
       (get m idx))))

(defn range-map [kind & kvs]
  {:pre [(or (= kind :find-ceiling)
             (= kind :find-floor))]}
  (->RangeMap kind (apply sorted-map kvs)))


(defrecord B+Tree [b slots])

  (search [this k]))


(deftype RangeMap [kind ^clojure.lang.IPersistentMap m leftfn rightfn]

  clojure.lang.IPersistentMap

  (assoc [this k v]
    (->RangeMap kind (.assoc m k v) leftfn rightfn))

  (assocEx [this k v]
    (->RangeMap kind (.assocEx m k v) leftfn rightfn))


  clojure.lang.ILookup
