implemented a second version of the algorithm