New distance implementation