Optimizing matrix class