refactor, handle protocol messages in their own functions (will move into separate file soon)