Refactoring for delayed output mostly done. Bugs remain.