 
 
   (iters-next [this]
-              ;;try next on the last iterator
-              (let [last (aget iterators (- len 1))]
-                (.next last)
-                (if-not (.-end? last)
-                  (ainto next-key (.key last))
-                  ;;otherwise walk up and get the min next value
-                  (loop [ix (- len 2)]
-                    (when (>= ix 0)
-                      (let [cur (aget iterators ix)]
-                        (.next cur)
-                        (if (.-end? cur)
-                          (recur (- ix 1))
-                          (ainto next-key (.make-min cur)))))))))
+              (loop [ix (- len 1)]
+                (when (>= ix 0)
+                  (let [cur (aget iterators ix)]
+                    (.next cur)
+                    (if (.-end? cur)
+                      (recur (- ix 1))
+                      (ainto next-key (.make-min cur)))))))
 
   (seek-join [this]
              ;;while we haven't found a match
              (loop []
-               ;;seek each iterator
-               (loop [ix 0]
-                 (if (< ix len)
-                   (let [cur (aget iterators ix)
-                         cur-key (.key cur)]
-                     (.seek cur next-key)
-                     (if-not (.-end? cur)
-                       (do
-                         (ainto next-key (.key cur))
-                         (recur (+ 1 ix)))
-                       (set! end? true)))))
-               (when (identical? end? false)
-                 (let [root-key (.key (aget iterators 0))
-                       last-key (.key (aget iterators (- len 1)))]
-                   ;;when the first and last keys are the same, we have a match
-                   (if (key= root-key last-key)
-                     (do
-                       (set! cur-key (.slice last-key 0))
-                       (.iters-next this))
-                     ;;otherwise recur
-                     (do
-                       (ainto next-key last-key)
-                       (recur )))
-                       ))))
+                   ;;seek each iterator
+                   (loop [ix 0]
+                     (if (< ix len)
+                       (let [cur (aget iterators ix)
+                             cur-key (.key cur)]
+                         (.seek cur next-key)
+                         (if-not (.-end? cur)
+                           (do
+                             (ainto next-key (.key cur))
+                             (recur (+ 1 ix)))
+                           (set! end? true)))))
+                   (when (identical? end? false)
+                     (let [root-key (.key (aget iterators 0))]
+                       ;;when the first and last keys are the same, we have a match
+                       (if (key= root-key next-key)
+                         (do
+                           (set! cur-key (.slice next-key 0))
+                           (.iters-next this))
+                         ;;otherwise recur
+                         (recur))))))
 
   (next [this]
         (.seek-join this))

                   (.mark this cur)))

  (check-rewind [this key]
                (let [len (alength nil-ixs)]
                  (loop [ix 0]
                    (if (< ix len)
                      (let [cur (aget nil-ixs ix)
                            rewind? (not (identical? (aget key cur) (aget prev-seek cur)))]
                        (cond
                         ;; we can't store anything if we're at the root value, so just rewind if we need

                (> a b))
           (> (typeof a) (typeof b)))))

(deftype MagicIterator [iterator map nil-ixs rewindable map-len ^:mutable cur-key ^:mutable cur-seek ^:mutable prev-seek ^:mutable marked-nodes ^:mutable marked-ixs ^:mutable end?]
  Object
  (mark [this i]
        (aset marked-nodes i (.-node iterator))
