replaced breakable loops with tail recursion