 
 
   (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))

(defn magic-iterator [tree map]
   (let [nil-ixs (array)
         itr (iterator tree)
         marked-nodes (js-obj 0 (.-node itr))
         marked-ixs (js-obj 0 0)
         magic (MagicIterator. itr map nil-ixs (alength map) (array) (.slice (or (.key itr) (array)) 0) (array) marked-nodes marked-ixs false)]
     (dotimes [ix (alength map)]
       (when (identical? nil (aget map ix))
         (.push nil-ixs ix)
         (aset marked-nodes ix (.-node itr))
         (aset marked-ixs ix (.-ix itr))))
     (.set-key magic)
     (.make-min magic)
     (when (identical? (.-end? magic) false)

        (map vec #js [#js [-3 -3] #js [-3 "3"]  #js ["3" -3] #js ["3" "3"] ])))
    )

    (let [tree1 (tree 10)
        _ (doseq [x [#js ["3" "4"]
                     #js [-3 -4]
                     ]]
            (.assoc! tree1 x 0))
        tree2 (tree 10)
        _ (doseq [x [#js ["3" "4"]
                     #js [-3 -4]
                     ]]
            (.assoc! tree2 x 0))
        itr1 (magic-iterator tree1 #js [0 1 nil nil])
        itr2 (magic-iterator tree2 #js [nil nil 0 1])
        _ (.clear js/console)
        join-itr (join-iterator #js [itr1 itr2])
        ]
;;     (assert
;;      (= (map vec (all-join-results join-itr))
;;         (map vec #js [#js [-3 -3] #js [-3 "3"]  #js ["3" -3] #js ["3" "3"] ])))

      (all-join-results join-itr)
    )


    (let [tree1 (tree 10)
        _ (doseq [x [#js ["1" "-5" "-5"]
                     #js [1 -6 "-6"]
                     #js ["-8" 2 "1"]
                     ]]
            (.assoc! tree1 x 0))
        _ (.clear js/console)
        itr1 (magic-iterator tree1 #js [0 1 2 nil nil nil])
        itr2 (magic-iterator tree1 #js [nil nil nil 0 1 2])
        join-itr (join-iterator #js [itr1 itr2])
        ]
;;     (assert
;;      (= (map vec (all-join-results join-itr))
;;         (map vec #js [#js [-3 -3] #js [-3 "3"]  #js ["3" -3] #js ["3" "3"] ])))

      (alength (all-join-results join-itr))
    )

  (let [tree1 (tree 10)
        _ (dotimes [i 10]
            (.assoc! tree1 #js [(js/Math.sin i) (js/Math.cos i) (js/Math.tan i)] i))
