move sleep() to the beginning of the loop