Refactored server shutdown