Improved distance function, improved testing, improved accuracy