working algorithm.