refactor button logic to handle some edge cases.