Refactoring for better error reporting