Add invariants support.
Various fixes.