Introduced some event stuff.  Cleaned up some ugly code blocks.