                         {:layers []
                          :succs {}
                          :preds {}
-                         :p #{}
-                         :q #{}
-                         :r #{}
+                         :ps #{}
+                         :qs #{}
+                         :rs #{}
                          :marked #{}
                          :last-nodes-by-character {}
                          :last-nodes-by-node {}}))

        g (-> graph
              (update-in [:layers p-layer-id :nodes] conj p-node)
              (update-in [:layers q-layer-id :nodes] conj q-node)
              (update-in [:p] conj p-node)
              (update-in [:q] conj q-node)
              (add-edge last-node p-node characters)
              (add-edge p-node q-node characters))]
    [g q-node]))

  "Step 1 of ESK - replace all p nodes with edges and merge segment containers"
  [graph layer]
  (->> (:ordered layer)
       (map #(if (contains? (:ps graph) %)
               ;; p nodes always have only one successor
               [(-> (:succs graph) (get %) first)]
               %))
