         let firstPoint = startingNode.pathComponent.point(at: startingNode.componentLocation)
         var points: [CGPoint] = [firstPoint]
         var orders: [Int] = []
-        func visit(from node: Node) -> Node? {
-            if let edge = node.forwardEdge, edge.inSolution, edge.visited == false {
-                let component = edge.component
-                visit(component)
-                edge.visited = true
-                return edge.forwardNode
-            }
-            if let edge = node.backwardEdge, edge.inSolution, edge.visited == false {
-                let component = edge.component.reversed()
-                visit(component)
-                edge.visited = true
-                return edge.backwardNode
-            }
-            return nil
-        }
-        func visit(_ component: PathComponent) {
+        func edgeNeedsVisiting(_ edge: Edge) -> Bool {
+            return edge.inSolution == true && edge.visited == false
+        }
+        func appendComponent(_ component: PathComponent) {
             points += component.points[1..<component.points.count]
             orders += component.orders
+        }
+        func visitEdge(_ edge: Edge, forwards: Bool) -> Node {
+            let component = edge.component
+            appendComponent(forwards ? component : component.reversed())
+            edge.visited = true
+            return forwards ? edge.endingNode : edge.startingNode
+        }
+        func visit(from node: Node) -> Node? {
+            if let edge = node.forwardEdge, edgeNeedsVisiting(edge) {
+                return visitEdge(edge, forwards: true)
+            } else if let edge = node.backwardEdge, edgeNeedsVisiting(edge) {
+                return visitEdge(edge, forwards: false)
+            }
+            return nil
         }
         var currentNode = startingNode
         repeat {

        if component.isClosed {
            let last = endCappedIntersections.last!
            let first = endCappedIntersections.first!
            if let secondToLast = last.backwardEdge?.backwardNode {
                let edge = Edge(forwardNode: first, backwardNode: secondToLast)
                secondToLast.forwardEdge = edge
                first.backwardEdge = edge
            }

            endCappedIntersections.append(Node(location: endingLocation, pathComponent: component))
        }
        let edges = (1..<endCappedIntersections.count).map {
            Edge(startingNode: endCappedIntersections[$0-1], endingNode: endCappedIntersections[$0])
        }
        for i in 0..<endCappedIntersections.count {
            if i > 0 {
