weight_matrix refactoring