Refactored implementation of undirectedtree