Fix hideous global abuse
Shift some event logic into a module
Stop wasting stack frames