Changed implementation to use 1D array for matrix