Improved logic for buttons