refactored websocket handler