First working algorithm.