Refactored arduino code and added comments