Fixed bugs in matrix