Refactor server code