Added recursive algorithm to find matched element tags.