Updated original method based on feedback to improve efficiency, added the inverse method (andGroupedOr) also