Refined TreeGen(NEEDS TO BE OPTIMIZED 4 LAG)