Refactore server code