Add the ability to inject arbitrary functions into the main loop.

Fixed #15.