Changed objects to pointers and moved event code to function