reorganizing the matrix code