added algorithm for dist