Major refactor for websocket support