fixed infinite loop in recursion