Moved the event handler below main and other small functions because it is huge.