wrote the sleep algorithm