Refactoring pre which

Merging Tom's code before we open source it.