bug in checking for tree.  didn't work if I only had one.  also some local functions...  not in the right place.