Handle asyncronous event with 'event' lib instead of global variables. Cleanup code too.