refactor button state checking into a single loop