Refactoring for more granular error handling