Refactoring to use upstream error handling code for consistency