Move class matching logic into helper function