refactor the server code