add flushing logic