General tidy up - lots more comments and debug, fixed some issues around
the event handling