refactored receive loop