refactoring and adding function on error