consolidate tree walking loops and add copyToStream method
Merge branch 'master' into topic/bench