Algorithm, added recursion