working code with Matrix