Fixed doSleep function