use generic function for tree walking