Changing order of code to make more sense (poll for event before testing event type in while loop)