                    (if-not (nil? map-ix)
                      (aset cur-key ix (aget found map-ix))
                      (aset cur-key ix nil)))
-                 (recur (inc ix))))))
+                 (recur (+ 1 ix))))))
   (seek [this key]
-        (loop [ix 0]
-          (when (< ix map-len)
-            (let [map-ix (aget map ix)]
-              (when-not (== map-ix nil)
-                (aset cur-seek map-ix (aget key ix))))
-            (recur (inc ix))))
-        (.seek iterator cur-seek)
-        (.set-key this)
+        (let [ros (rewind-or-store key prev-seek nil-ixs)
+              ros-ix (aget ros 0)]
+          (when (identical? (aget ros 2) true)
+            (.rewind this (aget ros 0)))
+          (when (identical? (aget ros 1) true)
+            (dotimes [x (alength cur-seek)]
+              (aset cur-seek x false))
+            (aset cur-seek (aget map ros-ix) (aget key ros-ix))
+            (.seek iterator cur-seek)
+            (.mark this ros-ix)
+            (.set-key this))
+          (when (identical? -1 (nilless-key-compare (.-cur-key this) key))
+            (loop [ix 0]
+              (when (< ix map-len)
+                (let [map-ix (aget map ix)]
+                  (when-not (== map-ix nil)
+                    (aset cur-seek map-ix (aget key ix))))
+                (recur (+ 1 ix))))
+            (.seek iterator cur-seek)
+            (.set-key this))
+          (set! prev-seek key)
+          )
         ))
 
-(deftype MagicIteratorWrapper [iterator map ^:mutable marked]
+(deftype MagicIteratorWrapper [iterator map]
   Object
-  (mark [this]
-        (set! marked node))
-  (rewind [this]
-          (set! (.-node iterator) marked)
-          (set! (.-ix iterator) 0)
-          (set! (.-end? iterator) false))
   (key [this]
        (.key iterator))
   (val [this]

    (if (< ix len)
      (let [cur (aget iterators ix)
            cur-key (.key cur)]
        (when (key-lt (fill! (.slice cur-key 0) key) key)
          (.seek cur key))
        (when-not (.-iterator.end? cur)
          (recur (inc ix))))
      true)))

(defn join [iterators]

  (:require [aurora.btree :refer [tree iterator key-lt key-lte key-gt key-gte]])
  (:require-macros [aurora.macros :refer [typeof]]))

(defn nilless-lt [a b]
  (if (== a nil)
    false
    (or (and (identical? (typeof a) (typeof b))
             (< a b))
        (< (typeof a) (typeof b)))))

(defn gt [a b]
  (and (not (identical? a b))
       (or (and (identical? (typeof a) (typeof b))
                (> a b))
           (> (typeof a) (typeof b)))))

(defn lt [a b]
  (and (not (identical? a b))
       (or (and (identical? (typeof a) (typeof b))
                (< a b))
           (< (typeof a) (typeof b)))))

(defn nilless-key-compare [as bs]
  (let [as-len (alength as)
        bs-len (alength bs)]
    (assert (== as-len bs-len) (pr-str as bs))
    (loop [i 0]
      (if (< i as-len)
        (let [a (aget as i)
              b (aget bs i)]
          (if (or (identical? a nil)
                  (identical? b nil))
            (recur (+ i 1))
            (if (identical? a b)
              (recur (+ i 1))
              (if (lt a b)
                -1
                1))))
        0))))

(defn rewind-or-store
  "Returns #js[ix, store?, rewind?]"
  [as bs nil-ixs]
  (let [len (alength nil-ixs)]
    (loop [ix 0]
      (if (< ix len)
        (let [cur (aget nil-ixs ix)
              rewind? (gt (aget as cur) (aget bs cur))]
          (cond
           (and (identical? cur 0) rewind?) #js [0 false rewind?]
           (> cur 0) (let [store? (gt (aget as (- cur 1)) (aget bs (- cur 1)))]
                       (if store?
                         #js [cur store? false]
                         (if rewind?
                           #js [cur false rewind?]
                           (recur (+ 1 ix)))))
           :else (recur (+ 1 ix))))
        #js [0 false false]))))

(defn filled-key-compare [as bs fill]
  (let [as-len (alength as)
