Fix exceptions and constant matrices handling. Incorporates Gabriel's changes.