Add dimension checking and matrix reference