more generic solution to matchNestedElementOfList