Refactor code to be more in line with socket.io example