Error handling refactored