Revert "Revert "take file flush out of main""

This reverts commit c79e32dff265c4ee2a5b3f3a39a562432c15831a.