moved populate tree to a simpler global function