Added documentation and seperated events into its own function