added diagonal matrix check. might there be a faster way?