move sleep to loop end