flush instead commit and then commit once at the end
refactoring