I should say that this example does not actually reflect the simplest or most efficient way to achieve what it does. (You really could really do it all in one search with a couple of levels escaped.) But I hope it illustrates some useful methods to achieve what you're after.