Fixed a bug in the matrix producers. And refactored code.