Try debugging the code by adding println statements that print messages as the code is executed so you can see when and where events happen.