Index: /issm/trunk/src/c/Bamgx/objects/Geometry.cpp
===================================================================
--- /issm/trunk/src/c/Bamgx/objects/Geometry.cpp	(revision 3298)
+++ /issm/trunk/src/c/Bamgx/objects/Geometry.cpp	(revision 3299)
@@ -141,5 +141,5 @@
 
 		//RequiredEdges
-		if(verbose>5) printf("      writing RequiredEdges\n");
+		if(verbose>5) printf("      writing %i RequiredEdges\n",nbreq);
 		bamggeom->NumRequiredEdges=nbreq;
 		if (nbreq){
@@ -160,10 +160,10 @@
 
 		//RequiredVertices
-		if(verbose>5) printf("      writing RequiredVertices\n");
+		if(verbose>5) printf("      writing %i RequiredVertices\n",nbreqv);
 		bamggeom->NumRequiredVertices=nbreqv;
 		if (nbreqv){
 			bamggeom->RequiredVertices=(double*)xmalloc(1*nbreqv*sizeof(double));
 			count=0;
-			for (i=0;i<nbe;i++){
+			for (i=0;i<nbv;i++){
 				if (vertices[i].Required()){
 					bamggeom->RequiredVertices[count]=i+1; //back to Matlab indexing
@@ -296,4 +296,5 @@
 			for (i=0;i<nbe;i++){
 
+				//printf("%i th edge of geometry: [%i %i]\n",i+1,(int)bamggeom->Edges[i*3+0],(int)bamggeom->Edges[i*3+1]);
 				i1=(int)bamggeom->Edges[i*3+0]-1; //-1 for C indexing
 				i2=(int)bamggeom->Edges[i*3+1]-1; //-1 for C indexing
@@ -789,5 +790,5 @@
 						 GeometricalEdge *e = & ei;
 						 GeometricalVertex *a=(*e)(k0); // begin 
-						 if(curves) {
+						 if(curves){
 							 curves[NbOfCurves].be=e;
 							 curves[NbOfCurves].kb=k0;
@@ -814,8 +815,6 @@
 				 }
 			 } 
-			if (nbgem==0 || nbe==0){
-				throw ErrorException(__FUNCT__,exprintf("nbgem==0 || nbe==0"));
-			}
-			if(step==0) {
+			assert(nbgem && nbe);
+			if(step==0){
 				curves = new Curve[NbOfCurves];
 			}
Index: /issm/trunk/src/c/Bamgx/objects/Geometry.h
===================================================================
--- /issm/trunk/src/c/Bamgx/objects/Geometry.h	(revision 3298)
+++ /issm/trunk/src/c/Bamgx/objects/Geometry.h	(revision 3299)
@@ -22,5 +22,4 @@
 namespace bamg {
 
-   /*CLASS: Geometry{{{1*/
 	class Geometry { 
 
@@ -75,5 +74,4 @@
 			void WriteGeometry(BamgGeom* bamggeom, BamgOpts* bamgopts);
 	};
-	/*}}}1*/
 	
 }
Index: /issm/trunk/src/c/Bamgx/objects/Triangles.cpp
===================================================================
--- /issm/trunk/src/c/Bamgx/objects/Triangles.cpp	(revision 3298)
+++ /issm/trunk/src/c/Bamgx/objects/Triangles.cpp	(revision 3299)
@@ -2681,12 +2681,15 @@
 		}
 
-		//check that edges is empty
+		//check that edges has been allocated
 		if (edges){
 			throw ErrorException(__FUNCT__,exprintf("edges is empty"));
 		}
 
-		// 2 step 
-		// step=0 to compute the number of edges + alloc at end
-		// step=1 to construct the edges
+		/* Now we are going to create the first edges corresponding
+		 * to the one present in the geometry provided.
+		 * We proceed in 2 steps
+		 *  -step 1: we count all the edges
+		 *           we allocate the number of edges at the end of step 1
+		 *  -step 2: the edges are created */
 		for (int step=0;step<2;step++){
 
@@ -2696,5 +2699,5 @@
 			long NbVerticesOnGeomEdge0=NbVerticesOnGeomEdge;
 			Gh.UnMarkEdges();	
-			NbOfCurves = 0;
+			NbOfCurves=0;
 
 			//go through the edges of the geometry
@@ -2704,17 +2707,20 @@
 				GeometricalEdge &ei=Gh.edges[i];   
 
-				// a good curve (not dup)
+				// a good curve (not a duplicate)
 				if (!ei.Dup()){ 
 
 					for(int j=0;j<2;j++) {
 
-						if (!ei.Mark() && ei[j].Required()) { 
-							// warning ei.Mark() can be change in loop for(j=0;j<2;j++) 
-							long nbvend=0;
+						/*The first time, no edge is marked but this might change during the loop*/
+						if (!ei.Mark() && ei[j].Required()){ 
+
+							long  nbvend=0;
 							Edge* PreviousNewEdge=NULL;
 
-							//do not create points if required
 							lstep = -1;
+
+							/*If Edge is required*/
 							if(ei.Required()){
+								//do not create internal points if required (take it as is)
 								if (j==0){
 									if(step==0) nbe++;
@@ -2739,12 +2745,10 @@
 							}
 
-							//if not required: on curve
+							/*If Edge is not required: on a curve*/
 							else {
-								for ( int kstep=0;kstep<= step;kstep++){
-									//step=0, nohing
-									//step=1,
-									//step=2
-									// 1 compute the length of the curve
-									// 2 create the points and edge
+								for ( int kstep=0;kstep<=step;kstep++){
+									//step=0, do nothing
+									//step=1, compute the length of the curve
+									//step=2  create the points and edge
 									PreviousNewEdge=0;
 									NbNewPoints=0;
@@ -2758,9 +2762,9 @@
 									// i = edge number, j=[0;1] vertex number in edge
 									
-									k=j;            // k = vertex number in edge
-									e=&ei;          // e = address of current edge
+									k=j;            // k = vertex number in edge (0 or 1)
+									e=&ei;          // e = reference of current edge
 									a=ei(k)->The(); // a = pointer toward the kth vertex of the current edge
-									va = a->to;     // va= pointer toward vertex of ???
-									e->SetMark();   // Mark e
+									va = a->to;     // va = pointer toward vertex associated
+									e->SetMark();   // Mark edge
 
 									//if SameGeo We have go to the background geometry 
@@ -2768,6 +2772,6 @@
 									for(;;){ 
 										k = 1-k;
-										b = (*e)(k)->The(); // b = pointer toward the other vertex of the current edge
-										AB = b->r - a->r;   // AB = vector of the current edge
+										b = (*e)(k)->The();// b = pointer toward the other vertex of the current edge
+										AB= b->r - a->r;   // AB = vector of the current edge
 										Metric MA = background ? BTh.MetricAt(a->r) :a->m ;  //Get metric associated to A
 										Metric MB =  background ? BTh.MetricAt(b->r) :b->m ; //Get metric associated to B
@@ -2811,5 +2815,4 @@
 
 										//Now, create corresponding points
-										//s = lstep
 										while (lcurve<=s && s <= lcurveb && nbv < nbvend){
 
@@ -2846,5 +2849,5 @@
 											edges[nbe].v[0]=va;
 											edges[nbe].v[1]=vb;
-											edges[nbe].ref = e->ref;
+											edges[nbe].ref =e->ref;
 											edges[nbe].onGeometry = e;
 											edges[nbe].adj[0] = PreviousNewEdge;
@@ -2887,5 +2890,5 @@
 								else nbe += NbEdgeCurve;
 							} // end on  curve ---
-						} // if (edges[i][j].Corner())  
+						}
 					}
 				}
@@ -3023,10 +3026,17 @@
 			Edge &ei = BTh.edges[iedge];
 			for(int je=0;je<2;je++){ // for the 2 extremities
-				if (!ei.onGeometry->Mark() && ei[je].onGeometry->IsRequiredVertex() ){
-					// a begin of curve 
+
+				// If one of the vertex is required we are in a new curve
+				if (ei[je].onGeometry->IsRequiredVertex()){ 
+
+					//Get curve number
 					int nc=ei.onGeometry->CurveNumber;
-					if(ei.onGeometry==Gh.curves[nc].be    && 
-								(GeometricalVertex *) *ei[je].onGeometry == &(*Gh.curves[nc].be)[Gh.curves[nc].kb] //  same extremity 
+					
+					//BUG FIX from original bamg
+					if(
+								(ei.onGeometry==Gh.curves[nc].be  || ei.onGeometry==Gh.curves[nc].ee)  &&          //Check that we are on the right edge
+								(GeometricalVertex *) *ei[je].onGeometry == &(*Gh.curves[nc].be)[Gh.curves[nc].kb] //Check that we are on the right extremity
 					  ){ 
+					if( (GeometricalVertex *) *ei[je].onGeometry == &(*Gh.curves[nc].be)[Gh.curves[nc].kb]){ 
 						bcurve[nc]=iedge*2+je;
 						bfind++;	
@@ -3035,5 +3045,7 @@
 			}
 		} 
-		assert(bfind==Gh.NbOfCurves);
+		if (bfind!=Gh.NbOfCurves){
+			throw ErrorException(__FUNCT__,exprintf("problem generating number of curves (Gh.NbOfCurves=%i bfind=%i)",Gh.NbOfCurves,bfind));
+		}
 
 		// method in 2 + 1 step 
Index: /issm/trunk/src/c/Bamgx/objects/Vertex.h
===================================================================
--- /issm/trunk/src/c/Bamgx/objects/Vertex.h	(revision 3298)
+++ /issm/trunk/src/c/Bamgx/objects/Vertex.h	(revision 3299)
@@ -30,7 +30,7 @@
 			short vint;  // the vertex number in triangle; varies between 0 and 2 in t
 			union {
-				Triangle* t; // one triangle which is containing the vertex
+				Triangle* t;   // one triangle which is containing the vertex
 				long      color;
-				Vertex*   to;  // use in geometry Vertex to now the Mesh Vertex associed 
+				Vertex*   to;  // used in geometry Vertex to know the Mesh Vertex associated 
 				VertexOnGeom* onGeometry;        // if vint == 8; // set with Triangles::SetVertexFieldOn()
 				Vertex*       onBackgroundVertex;// if vint == 16 on Background vertex Triangles::SetVertexFieldOnBTh()
