Fix an issue with caching the best cost.