Optimized code for button behavior.