avoid over recursion call by transform recursion to loop