Fix implementation of distance