Fixed write/flush ordering bug