cleaned up code and refactored timeout