Fix contiguous optimization
Add implementations for pairwise distances