refactored and added connection sleeping