server code refactored