Added second improved algorithm.