Minor efficiency improvement: break out of the loop once you have found the group.