Refactored error handling.