major rewrite, use socket.io