fixed list-to-tree function