Refactoring the handler code