Refactored error handling mechanism