refactor tree api to use foreach / walk