correcting the loop end