refactored wait