More refactoring of the server handle function.