Improving websockets code