Refactor to cope better with stampede.

Also centralise logging via events.