Added code to handle matrix incompatibility.