Fix error handling bug