First working algorithm #3