Fixed detecting if at the end of the tree.