Added simplification patterns.
Fixed major bugs in matrix code.