encapsulated matrix class and made a few improvements