refactored flush and read