Refactoring server logic