first implementation of algorithm