Making error handling more robust