Python provides some nifty tools for mocking, without relying on altering running code. We should use these to simplify the actual run paths and move the complicated logic into tests.