Refactoring the handlers code to remove duplicate functions