optimised a few loops in the tree

Seems that with large trees this might make some differences. It has for
me in the past.