Moved the matrix code into a separate module.