Index: /issm/trunk/src/m/classes/public/mesh/findsegments.m
===================================================================
--- /issm/trunk/src/m/classes/public/mesh/findsegments.m	(revision 2611)
+++ /issm/trunk/src/m/classes/public/mesh/findsegments.m	(revision 2612)
@@ -21,17 +21,29 @@
 count=1;
 
+%loop over the segments
 for i=1:num_segments,
+
+	%get current element on boundary
 	el1=pos(i);
+
+	%get elements connected to el1
 	els2=elementconnectivity(el1,find(elementconnectivity(el1,:)));
+
+	%el1 is connected to 2 other elements
 	if length(els2)>1,
+
+		%get nodes of el1
+		nods1=md.elements(el1,:);
+
+		%find the common vertices to the two elements connected to el1 (1 or 2)
 		flag=intersect(md.elements(els2(1),:),md.elements(els2(2),:));
-		nods1=md.elements(el1,:);
-		nods1(find(nods1==flag))=[];
+
+		%get the vertices on the boundary and build segment
+		nods1(find(ismember(nods1,flag)))=[];
 		segments(count,:)=[nods1 el1];
 
+		%swap segment grids if necessary
 		ord1=find(nods1(1)==md.elements(el1,:));
 		ord2=find(nods1(2)==md.elements(el1,:));
-
-		%swap segment grids if necessary
 		if ( (ord1==1 & ord2==2) | (ord1==2 & ord2==3) | (ord1==3 & ord2==1) ),
 			temp=segments(count,1);
@@ -41,11 +53,20 @@
 		segments(count,1:2)=fliplr(segments(count,1:2));
 		count=count+1;
+
+	%el1 is connected to only one element
 	else
+		%get nodes of el1
 		nods1=md.elements(el1,:);
+
+		%find the vertex  the el1 to not share with els2
 		flag=setdiff(nods1,md.elements(els2,:));
+
 		for j=1:3,
 			nods=nods1; nods(j)=[];
 			if any(ismember(flag,nods)),
+
 				segments(count,:)=[nods el1];
+
+				%swap segment grids if necessary
 				ord1=find(nods(1)==md.elements(el1,:));
 				ord2=find(nods(2)==md.elements(el1,:));
