Implemented D2 distance