Refactored websocket_handler