Refactored button listener code