Refactored exception handling