matrix unit testing and bug fixes