working on the algorithm