fixing the algorithm; now works with max distance too. Big change, now logs routings too