Implementing algorithm.