Refactor Matrix module