refactor Matrix