Refactored and commented the server code.