Flush on write; minor refactoring