refactoring with error messages