added implementation of the algorithm