#pragma once needs to be above includes
diff --git a/gen/src/include.rs b/gen/src/include.rs
index 4309c9c..54a359f 100644
--- a/gen/src/include.rs
+++ b/gen/src/include.rs
@@ -127,9 +127,6 @@
             writeln!(f, "#include <BaseTsd.h>")?;
             writeln!(f, "#endif")?;
         }
-        if *self != Self::default() {
-            writeln!(f)?;
-        }
         Ok(())
     }
 }
diff --git a/gen/src/out.rs b/gen/src/out.rs
index 08bf85f..f07a0d3 100644
--- a/gen/src/out.rs
+++ b/gen/src/out.rs
@@ -51,16 +51,15 @@
         }
     }
 
-    pub fn prepend(&mut self, section: String) {
-        let content = self.content.get_mut();
-        content.bytes.splice(..0, section.into_bytes());
-    }
-
     pub fn write_fmt(&self, args: Arguments) {
         let content = &mut *self.content.borrow_mut();
         Write::write_fmt(content, args).unwrap();
     }
 
+    pub fn extend(&self, other: &Self) {
+        self.content.borrow_mut().write_bytes(&other.content.borrow().bytes);
+    }
+
     pub fn content(&self) -> Vec<u8> {
         self.content.borrow().bytes.clone()
     }
@@ -68,7 +67,14 @@
 
 impl Write for Content {
     fn write_str(&mut self, s: &str) -> fmt::Result {
-        if !s.is_empty() {
+        self.write_bytes(s.as_bytes());
+        Ok(())
+    }
+}
+
+impl Content {
+    fn write_bytes(&mut self, b: &[u8]) {
+        if !b.is_empty() {
             if !self.blocks_pending.is_empty() {
                 if !self.bytes.is_empty() {
                     self.bytes.push(b'\n');
@@ -84,8 +90,7 @@
                 }
                 self.section_pending = false;
             }
-            self.bytes.extend_from_slice(s.as_bytes());
+            self.bytes.extend_from_slice(b);
         }
-        Ok(())
     }
 }
diff --git a/gen/src/write.rs b/gen/src/write.rs
index 04d8e3e..88a0734 100644
--- a/gen/src/write.rs
+++ b/gen/src/write.rs
@@ -17,10 +17,6 @@
     let mut out_file = OutFile::new(namespace.clone(), header);
     let out = &mut out_file;
 
-    if header {
-        writeln!(out, "#pragma once");
-    }
-
     out.include.extend(opt.include);
     for api in apis {
         if let Api::Include(include) = api {
@@ -114,9 +110,17 @@
         write_generic_instantiations(out, types);
     }
 
-    out.prepend(out.include.to_string());
-
-    out_file
+    // We collected necessary includes lazily while generating the above. Now
+    // put it all together.
+    let mut full_file = OutFile::new(namespace.clone(), header);
+    let full = &mut full_file;
+    if header {
+        writeln!(full, "#pragma once");
+    }
+    write!(full, "{}", out.include);
+    full.next_section();
+    full.extend(out);
+    full_file
 }
 
 fn write_includes(out: &mut OutFile, types: &Types) {