// Find a good edge
            forAll(singleEdges, edgeI)
            {
                SLList<label> pointChain;

                bool blockHead = false;
                bool blockTail = false;

                if (!singleEdgeUsage[edgeI])
                {
                    // found a new edge
                    singleEdgeUsage[edgeI] = true;

                    label newEdgeStart = singleEdges[edgeI].start();
                    label newEdgeEnd = singleEdges[edgeI].end();

                    pointChain.insert(newEdgeStart);
                    pointChain.append(newEdgeEnd);

#                   ifdef DEBUG_CHAIN
                    Info<< "found edge to start with: "
                        << singleEdges[edgeI] << endl;
#                   endif

                    // Check if head or tail are blocked
                    forAll(cellPoints, pointI)
                    {
                        if (cellPoints[pointI] == newEdgeStart)
                        {
                            if (pointUsage[pointI] > 2)
                            {
#                               ifdef DEBUG_CHAIN
                                Info<< "start head blocked" << endl;
#                               endif

                                blockHead = true;
                            }
                        }
                        else if (cellPoints[pointI] == newEdgeEnd)
                        {
                            if (pointUsage[pointI] > 2)
                            {
#                               ifdef DEBUG_CHAIN
                                Info<< "start tail blocked" << endl;
#                               endif

                                blockTail = true;
                            }
                        }
                    }

                    bool stopSearching = false;

                    // Go through the unused edges and try to chain them up
                    do
                    {
                        stopSearching = false;

                        forAll(singleEdges, addEdgeI)
                        {
                            if (!singleEdgeUsage[addEdgeI])
                            {
                                // Grab start and end of the candidate
                                label addStart =
                                    singleEdges[addEdgeI].start();

                                label addEnd =
                                    singleEdges[addEdgeI].end();

#                               ifdef DEBUG_CHAIN
                                Info<< "Trying candidate "
                                    << singleEdges[addEdgeI] << endl;
#                               endif

                                // Try to add the edge onto the head
                                if (!blockHead)
                                {
                                    if (pointChain.first() == addStart)
                                    {
                                        // Added at start mark as used
                                        pointChain.insert(addEnd);

                                        singleEdgeUsage[addEdgeI] = true;
                                    }
                                    else if (pointChain.first() == addEnd)
                                    {
                                        pointChain.insert(addStart);

                                        singleEdgeUsage[addEdgeI] = true;
                                    }
                                }

                                // Try the other end only if the first end
                                // did not add it
                                if (!blockTail && !singleEdgeUsage[addEdgeI])
                                {
                                    if (pointChain.last() == addStart)
                                    {
                                        // Added at start mark as used
                                        pointChain.append(addEnd);

                                        singleEdgeUsage[addEdgeI] = true;
                                    }
                                    else if (pointChain.last() == addEnd)
                                    {
                                        pointChain.append(addStart);

                                        singleEdgeUsage[addEdgeI] = true;
                                    }
                                }

                                // check if the new head or tail are blocked
                                label curEdgeStart = pointChain.first();
                                label curEdgeEnd = pointChain.last();

#                               ifdef DEBUG_CHAIN
                                Info<< "curEdgeStart: " << curEdgeStart
                                    << " curEdgeEnd: " << curEdgeEnd << endl;
#                               endif

                                forAll(cellPoints, pointI)
                                {
                                    if (cellPoints[pointI] == curEdgeStart)
                                    {
                                        if (pointUsage[pointI] > 2)
                                        {
#                                           ifdef DEBUG_CHAIN
                                            Info<< "head blocked" << endl;
#                                           endif

                                            blockHead = true;
                                        }
                                    }
                                    else if (cellPoints[pointI] == curEdgeEnd)
                                    {
                                        if (pointUsage[pointI] > 2)
                                        {
#                                           ifdef DEBUG_CHAIN
                                            Info<< "tail blocked" << endl;
#                                           endif

                                            blockTail = true;
                                        }
                                    }
                                }

                                // Check if the loop is closed
                                if (curEdgeStart == curEdgeEnd)
                                {
#                                   ifdef DEBUG_CHAIN
                                    Info<< "closed loop" << endl;
#                                   endif

                                    pointChain.removeHead();

                                    blockHead = true;
                                    blockTail = true;

                                    stopSearching = true;
                                }

#                               ifdef DEBUG_CHAIN
                                Info<< "current pointChain: " << pointChain
                                    << endl;
#                               endif

                                if (stopSearching) break;
                            }
                        }
                    } while (stopSearching);
                }