minor refactoring and addition of docstrings to matrix module.