support onexit function callbacks

this was done fast and is not entirely correct. and the code that
handles the callbacks is sloppy.

refs #219