distance logic