Simplify and refactor server code