     private func createComponent(from node: IntersectionNode) -> PathComponent {
         var points: [CGPoint] = [node.edge!.component.startingPoint]
         var orders = [Int]()
-
+        func visit(from node: IntersectionNode) -> IntersectionNode? {
+            guard let forwardEdge = node.edge else {
+                assertionFailure("consistency failure")
+                return nil
+            }
+            guard let backwardsEdge = node.previous?.edge else {
+                assertionFailure("consistency failure")
+                return nil
+            }
+            if forwardEdge.inSolution, forwardEdge.visited == false {
+                visit(forwardEdge.component)
+                forwardEdge.visited = true
+                return node.next
+            } else if backwardsEdge.inSolution, backwardsEdge.visited == false {
+                visit(backwardsEdge.component.reversed())
+                backwardsEdge.visited = true
+                return node.previous
+            } else {
+                return nil
+            }
+        }
         func visit(_ component: PathComponent) {
             points += component.points[1..<component.points.count]
             orders += component.orders
         }
+
         var nextNode: IntersectionNode? = node
         while let currentNode = nextNode {
             nextNode = nil
-            if currentNode.edge?.inSolution == true, currentNode.edge?.visited == false {
-                visit(node.edge!.component)
-                currentNode.edge?.visited = true
-                nextNode = currentNode.next
-            } else if currentNode.previous?.edge?.inSolution == true, currentNode.previous?.edge?.visited == false {
-                currentNode.previous?.edge?.visited = true
-                visit(currentNode.previous!.edge!.component.reversed())
-                nextNode = currentNode.previous
+            if let temp = visit(from: currentNode) {
+                nextNode = temp
             } else {
-
-                for neighbor in currentNode.neighbors.values.joined() {
-                    if nextNode != nil {
+                for neighbor in currentNode.neighbors {
+                    if let temp = visit(from: neighbor) {
+                        nextNode = temp
                         break
                     }
-                    if neighbor.edge!.inSolution == true, neighbor.edge!.visited == false {
-                        visit(neighbor.edge!.component)
-                        neighbor.edge!.visited = true
-                        nextNode = neighbor.next
-                    } else if neighbor.previous!.edge!.inSolution == true, neighbor.previous!.edge!.visited == false {
-                        neighbor.previous!.edge!.visited = true
-                        visit(neighbor.previous!.edge!.component.reversed())
-                        nextNode = neighbor.previous
-                    }
                 }
-
             }
         }
 
         if points.last != points.first {
-//            points.append(points.first!)
-//            orders.append(1)
+            points.append(points.first!)
+            orders.append(1)
         }
 
         return PathComponent(points: points, orders: orders)

            let first = endCappedIntersections.first!
            first.previous = last.previous
            last.previous?.next = first
            first.isIntersection = first.isIntersection || last.isIntersection
            endCappedIntersections.removeLast()
        }
        return endCappedIntersections.first!

    var next: IntersectionNode?
    var previous: IntersectionNode?
    var edge: Edge?
    /// links to locations on neighboring paths, indexed by path number
    var neighbors = [IntersectionNode]()
    init(location: IndexedPathLocation) {
        self.location = location
    }
    func addNeighbor(_ neighbor: IntersectionNode) {
        guard self.neighbors.allSatisfy({ $0 !== neighbor }) else { return }
        neighbors.append(neighbor)
    }
    func replaceNeighborReference(_ node: IntersectionNode, with replacement: IntersectionNode) {
        for i in neighbors.indices where neighbors[i] === node {
            neighbors[i] = replacement
        }
    }
    func mergeNeighbors(of node: IntersectionNode) {
        for neighbor in node.neighbors {
            neighbor.replaceNeighborReference(node, with: self)
            self.addNeighbor(neighbor)
        }
    }
}
