Refactoring arduino program