updated the client code to better handle events