Bug fix for error handling