Added recursive algorithm