Refactor matrix