remade  loop to recursion