Fixed error where getting from cache would result in wrong input for last foreach