More unit tests, safeguards and fixing for empty tree in IO and iterators