Completed second algorithm.