the algorithm now works