Switching off the pins code refactored into a loop