Clean-up some code and implement flush.