matrix refactoring