Add nmatrix ifdef and clean up procedure