Switch to socket.io and refactor