server refactoring
some bugs fixed