refactored traverse tree