Refactored server code