updated match calculation, still doesnt detect recursive matches