   equivalent to the position in the previous layer, so we just use that), and we merge
   the two lists into a single ordering based on these measures."
   [layer next-layer]
-  (let [minus-ps (:minus-ps layer)
-        positions (:positions layer)
-        non-qs (:non-qs next-layer)
-        measures (:measures layer)
-        ordered (loop [nodes (sort-by #(get measures %) non-qs)
-                       segments (sort-by #(get positions %) (take-nth 2 minus-ps))
-                       pos positions
-                       ord []]
-                  (if (or (empty? nodes) (empty? segments))
-                    ;; ESK's algorithm doesn't specify what to do with leftover things
-                    ;; I think this is because it doesn't take into account nodes that don't have
-                    ;; parents in layers > 0. In any case, at most one of rem-nodes or rem-segs
-                    ;; will be non-empty
-                    (-> ord (into nodes) (into (filter #(seq (:segments %)) segments)))
-                    (let [node-1 (first nodes)
-                          seg-1 (first segments)
-                          node-measure (get measures node-1)
-                          seg-position (get pos seg-1)
-                          node-first (<= node-measure seg-position)
-                          seg-first (>= node-measure (+ seg-position (count (:segments seg-1))))]
-                      (cond node-first (recur (rest nodes) segments pos (conj ord node-1))
-                            seg-first (recur nodes (rest segments) pos (conj ord seg-1))
-                            :else (let [k (.ceil js/Math (- node-measure seg-position))
-                                        s-1 (update-in seg-1 [:segments] rrb/subvec 0 k)
-                                        s-2 (update-in seg-1 [:segments] rrb/subvec k)]
-                                    (recur
-                                     (rest nodes)
-                                     (cons s-2 segments)
-                                     (assoc pos s-2 (+ (get pos seg-1) 1))
-                                     (into ord [s-1 node-1])))))))]
-    (assoc next-layer :minus-qs ordered)))
+  (let [positions (:positions layer)
+        measures (:measures next-layer)
+        ns (sort-by #(get measures %) (:non-qs next-layer))
+        ss (->> (:minus-ps layer)
+                (filter #(instance? SegmentContainer %))
+                (sort-by #(get positions %)))
+        minus-qs (loop [nodes ns
+                        segments ss
+                        pos positions
+                        ord []]
+                   (if (or (empty? nodes) (empty? segments))
+                     ;; ESK's algorithm doesn't specify what to do with leftover things
+                     ;; I think this is because it doesn't take into account nodes that don't have
+                     ;; parents in layers > 0. In any case, at most one of nodes or segments
+                     ;; will be non-empty
+                     (-> ord (into nodes) (into segments))
+                     (let [node-1 (first nodes)
+                           seg-1 (first segments)
+                           node-measure (get measures node-1)
+                           seg-position (get pos seg-1)
+                           node-first (<= node-measure seg-position)
+                           seg-first (>= node-measure (+ seg-position (count (:segments seg-1))))]
+                       (cond node-first (recur (rest nodes) segments pos (conj ord node-1))
+                             seg-first (recur nodes (rest segments) pos (conj ord seg-1))
+                             :else (let [k (.ceil js/Math (- node-measure seg-position))
+                                         s-1 (update-in seg-1 [:segments] rrb/subvec 0 k)
+                                         s-2 (update-in seg-1 [:segments] rrb/subvec k)]
+                                     (recur
+                                      (rest nodes)
+                                      (cons s-2 segments)
+                                      (assoc pos s-2 (+ (get pos seg-1) 1))
+                                      (into ord [s-1 node-1])))))))]
+    (assoc next-layer :minus-qs minus-qs)))
 
 
 (defn add-qs
   "Step 4 of ESK - takes the results of step 3, which doesn't include the q-nodes, and adds them,
   splitting their segment containers in the process"
   [next-layer]
-  (let [ordered (:minus-qs next-layer)
-        qs (:qs next-layer)
-        flat (->> ordered
+  (let [qs (:qs next-layer)
+        flat (->> (:minus-qs next-layer)
                   (mapcat #(if (instance? SegmentContainer %)
                              (:segments %)
                              [%]))

              ordered-graph (-> (if reverse?
                                  (reverse-graph sparse-graph)
                                  sparse-graph)
                                (update-in [:layers 0] assoc :alternating alternating)
                                order-graph-once)]
          (println c (:crossings ordered-graph) (-> ordered-graph :layers peek :alternating))
          (recur (-> ordered-graph :layers peek :alternating)
                 (conj orderings (if reverse? (reverse-graph ordered-graph) ordered-graph))))))))

                        (if (empty? (:segments seg-c))
                          (recur (rest f) seg-c (conj layer item))
                          (recur (rest f) (first segment-containers) (conj layer seg-c item)))))))]
    (assoc next-layer :ordered ordered)))


(defn- sorted-edge-order
