Refactoring & more integration testing

Lots of interesting stuff here. Walk through the commits.