The algorithm