Fixed up some of the code. This should simplify the execution logic, and provide better test coverage.