Implemented global setups and teardowns, updated docs.

Also did a number of refactorings which removed duplication in the code.