updated distances function