refactored matrix update matrix