Fixes and extensions of distance function