First working algorithm #2