algorithm. Distance computation is still a naive implementation and can be further sped up if necessary