fixing bug in timed loop