Simplified the recursive algorithm.