refactoring error handling