Some tidying of the handle_client function