 			ax = bx[i];
 			ay = by[i];
 			i -= 1;
-		} else { // Failure, so add midpoint to list
+		} else { // Failure, so add midpoint to list if
 			i += 1;
-			bx[i] = mx;
-			by[i] = my;
+			if (i < maxiter - 1) { // there is space left in buffer
+				bx[i] = mx;
+				by[i] = my;
+			} else { // there's no space in the buffer
+				bx[maxiter - 1] = mx;
+				by[maxiter - 1] = my;
+			}
 		}
 	}
 	return ival;

}


/*
int main(){
	double d[50] = {0.87755042,  0.90101722,  0.88297422,  0.90225072,  0.91185969,        0.88479424,  0.64257305,  0.23650566,  0.17532272,  0.24785572,        0.77647345,  0.12143413,  0.04994399,  0.19918067,  0.09625039,

        
	double sum = 0;
	
	sum = sum_log_likelihood(50,d,0.,1.,.05,3.,8.,.1,1e-20);
	double truth = -45.312935111611729;

	printf("Real : %.10f\n",truth);
	printf("Calcd: %.10f\n",sum);
	printf("%%Diff: %.10f\n",abs((truth-sum)/truth)*100.);
}
*/

