Fixed bugs in matrix.py and added more unit tests.