moving sleep to beginning of loop