Points shifted after Brep | Brep

Hi, I’m having a problem with my data tree structure after using the Brep difference component.
First, here’s the base structure I’m dealing with:

It’s a series of walls that rotate slightly every level. Basically I’m trying to find the region of overlap that runs throughout the walls and connect the four corners with a polyline. However, the points change orientation partway through the model, as you can see in the following picture.

What I did was to define all the walls as one huge group of Breps, then deconstruct them and use the List Item component to extract the bottom (touching below) and top (touching wall above) of each wall. I then used the Brep difference component to find the overlap, and then I exploded the overlap region, flipped the matrix, and connected the vertices.

As a test, I tried connecting the corners (using the same explode-flip-polyline method) of the bottoms of the walls before the Brep | Brep, and it worked fine:

When I zoomed in and used Point List, I found that somehow the points had shifted after the Brep difference.

Also, for some reason the first point is also the last point.

How do I fix it? Any idea? Here are the rhino and grasshopper files. (Rhino file contains the walls I used). Thanks again guys.
troubleshoot.gh (23.7 KB)
troubleshoot.3dm (225.2 KB)

Ok, can’t tell you why the Close Option duplicated the first Point and not connect the last with the first Point. So as I know, you always get “0->1; 1->2; 2->3; 3->4” and not “0->1; 1->2; 2->3; 3->0”. Maybe someone with Scripting experience can explain this. I assume that it has something to do with nurbs curves etc, because there you also have stored weights and vectors to the control points. But I only can guess. Back to your Problem.

If you don’t want the dupl. points, easily cull the last index.

To sort the points, you could use “sort along curve”

troubleshoot_re.gh (16.7 KB)