change sleep time and remove duplicate code