some performance improvements with distance functions