lookup -> entityLookup, fix count() in loop, extract getMatchingIds