Made event handler loop use a switch