Refactor code for websockets