corrected looping problem