Fix caching bug with loop