I changed the flush function a little bit.
I grab the lock outside.