fix inversion condition, environment extension; add some tests and documentation.