Slap: added some changes thanks to FUBAR.  Note: I kept the binary file mode ("wb" vs "w"), because I don't want it writing different things to Windows vs *nix (even if it looks wrong in notepad).