Adding a forever loop with a sleep, and reducing duplicate code