cleaned up the WebSocket code
manuelly merged commit before stream but applied some improvements I found