fixed copy tree algorithm