Refactor matrix.