refactoring error handler