refactoring of the error handling