Add caching to getCost