added a bit more to the algorithm