Refactoring and add some code to the server