Refactoring and additional error message with test