moved event handler behind if statement