Changed the logic to solve matrix
Added comments