now uses M*N matrix properly