Refactor: breakup websocket handlers into functions