Wrap generated code in an IIFE so we can declare bundle-level "globals"

Also added comments to explain the various stages of the build process