refactor server code