Automatically attach modules to events

And change the syntax of FOREACH macros to be less dumb.