refactor button event handling