Update to matrix class for better memory handling.