#16, #17 Fixed distance function