First working algorithm