Move the waterfall code into a class and make some other minor changes.