Improving logic of checking if matrix changed - it was heavily simplified, a ton of unnecessary code deleted