Bug fix on error handling