wait for file writing to finish
update flag handling