Some more refactor in matrix management code