some more refactoring on error handling