Pull out recursive legend item search into a utility function