Fixed a bug in the server and reorganized the code.