refactor response handler code