Improve algorithm's accuracy.

In some instances, the algorithm could terminate too soon.  Also, failed
to add a gap offset to one call.