edited cahematrix code