Fixed divide by zero bug in dist function