moved player event handling code into its own function