Engine

Here's the significantly cleaned up pull request for the engine refactoring.