Refactored client handler code