Huge refactoring to better support debugging and laziness