Make triggers actually work.
Tidy up trigger-handling code.