Added rollover handling in sleep function