Refactoring server code