Refactored server for simplicity and robustness.