added distance algorithm