Refactored the matrix code into a working class.