   (reset [this]
          (.reset iterator)))
 
-(defn trieterator [tree]
-  (let [it (iterator tree)]
-    (Trieterator. it (least-key (.-key-len tree)) nil -1 0 (.-end? it))))
-
-(deftype Intersection [iterators ^:mutable end?]
+(defn trieterator [iterator]
+  (Trieterator. iterator (least-key (.-key-len iterator)) nil -1 0 (.-end? it) (.-key-len iterator)))
+
+;; works on trees
+(deftype Intersection [iterators ^:mutable end? key-len]
   Object
   (key [this]
        (when (false? end?)

  (loop [node (.-root tree)]
    (if (nil? (.-children node))
      (if (> (alength (.-keys node)) 0)
        (Iterator. (.-max-keys tree) node 0 #js [] #js [] false)
        (Iterator. (.-max-keys tree) node 0 #js [] #js [] true))
      (recur (aget (.-children node) 0)))))

(deftype Trieterator [iterator seek-key ^:mutable pinned-key ^:mutable pinned-key-ix ^:mutable moving-key-ix ^:mutable end?]
  Object
  (key [this]
       (aget (.key iterator) moving-key-ix))

            (set! end? true)
            (.search this 1)))))

(defn intersection [iterators]
  (assert (> (alength iterators) 0))
  (assert (every? #(= (.-key-len (aget iterators 0)) (.-key-len %)) iterators))
  (if (> (alength iterators) 1)
    (if (some #(.-end? %) iterators)
      (Intersection. iterators true)
      (let [intersection (Intersection. (into-array (sort-by #(.key %) key-compare iterators)) false (.-key-len (aget iterators 0)))]
        (.search intersection 0)
        intersection))
    (aget iterators 0)))

;; works on tries
(deftype Join [^:mutable key-ix iterators key-ix->intersection key-ix->iterator->present? ^:mutable end? key-len]
  Object
  (key [this]
       (when (false? end?)
         (.key (aget key-ix->intersection key-ix))))
  (val [this]
       (when (false? end?)
         (.val (aget key-ix->intersection key-ix))))
  (maintain [this]
            (set! end? (.-end? (aget key-ix->intersection key-ix))))
  (next [this]
