Compartmentalise and optimise scripts, add checkEventHandler