Added set_pinmode/3
Extracted common part of pin loop into seperate function.