server code refactor