Index: /issm/trunk/src/c/objects/Bamg/Triangles.cpp
===================================================================
--- /issm/trunk/src/c/objects/Bamg/Triangles.cpp	(revision 5017)
+++ /issm/trunk/src/c/objects/Bamg/Triangles.cpp	(revision 5018)
@@ -2632,37 +2632,40 @@
 		Triangle * t=0;	
 		int j,jp,jn,jj;
-		if (tstart) t=tstart;
+		int counter;
+
+		/*Get starting triangle. Take tsart if provided*/
+		if (tstart){
+			t=tstart;
+		}
+		/*Else find the closest Triangle using the quadtree*/
 		else {
-			if (!quadtree){
-				ISSMERROR("no starting triangle provided and no quadtree available");
-			}
+
+			/*Check that the quadtree does exist*/
+			if (!quadtree) ISSMERROR("no starting triangle provided and no quadtree available");
+
+			/*Call NearestVertex*/
 			MeshVertex *a = quadtree->NearestVertex(B.x,B.y) ;
 
-			if (!a || !a->t ) {
-				if (a) {
-					printf("TriangleContaining vertex number %i, another call to ReMakeTriangleContainingTheVertex was required\n", Number(a));
-				}
-				ISSMERROR("problem in Triangles::FindTriangleContaining");
-			}
-			if (a<vertices || a>=vertices+nbv){
-				ISSMERROR("a<vertices || a>=vertices+nbv");
-			}
+			/*Check output (Vertex a)*/
+			if (!a)    ISSMERROR("problem while trying to find nearest vertex from a given point. No output found");
+			if (!a->t) ISSMERROR("no triangle is associated to vertex number %i (another call to ReMakeTriangleContainingTheVertex is required)",Number(a)+1);
+			ISSMASSERT(a>=vertices && a<vertices+nbv);
+
+			/*Get starting triangle*/
 			t = a->t;
-			if (t<triangles || t>=triangles+nbt){
-				a->Echo();
-				ISSMERROR("t<triangles || t>=triangles+nbt");
-			}
-		}
+			ISSMASSERT(t>=triangles && t<triangles+nbt);
+		}
+
 		Icoor2  detop ;
 
-		// number of test triangle 
-		int kkkk =0; 
-
-		// if the initial triangles is outside  
-		while ( t->det < 0){ 
-			int k0=(*t)(0) ?  ((  (*t)(1) ? ( (*t)(2) ? -1 : 2) : 1  )) : 0;
-			if (k0<0){ // k0 the NULL vertex
-				ISSMERROR("k0<0");
-			}
+		/*initialize number of test triangle*/
+		counter=0; 
+
+		/*The initial triangle might be outside*/
+		while (t->det < 0){ 
+
+			/*Get a real vertex from this triangle (k0)*/
+			int k0=(*t)(0)?(((*t)(1)?((*t)(2)?-1:2):1)):0;
+			ISSMASSERT(k0>=0);// k0 the NULL vertex
 			int k1=NextVertex[k0],k2=PreviousVertex[k0];
 			dete[k0]=det(B,(*t)[k1],(*t)[k2]);
@@ -2671,8 +2674,6 @@
 			 return t; 
 			t = t->TriangleAdj(OppositeEdge[k0]);
-			kkkk++;
-			if (kkkk>=2){
-				ISSMERROR("kkkk>=2");
-			}
+			counter++;
+			ISSMASSERT(counter<2);
 		}
 
@@ -2680,9 +2681,9 @@
 		detop = det(*(*t)(VerticesOfTriangularEdge[jj][0]),*(*t)(VerticesOfTriangularEdge[jj][1]),B);
 
-		while(t->det  > 0 ) { 
-			kkkk++;
-			if (kkkk>=2000){
-				ISSMERROR("kkkk>=2000");
-			}
+		while(t->det>0) { 
+
+			/*Increase counter*/
+			if (++counter>=10000) ISSMERROR("Maximum number of iteration reached (threshold = %i).",counter);
+
 			j= OppositeVertex[jj];
 			dete[j] = detop;  //det(*b,*s1,*s2);
@@ -2717,5 +2718,5 @@
 		if (t->det<0) // outside triangle 
 		 dete[0]=dete[1]=dete[2]=-1,dete[OppositeVertex[jj]]=detop;
-		//  NbOfTriangleSearchFind += kkkk;  
+		//  NbOfTriangleSearchFind += counter;  
 		return t;
 	}
