Refactor button code