add GM's suggested sorting fix(it seems to work, the only remaining artifacts are small gaps and errors of distance)