The algorithm from the article: