modified loop implementation for more accuracy.