major refactor. both client and server.