Added more code to make this an event driven system