refactored server code from console app