Optimized plugin/polyfill inner classes and events

Also fixes #3884.