DistMat refactoring