Added error handling code for arduino, as well as a small bit of refactoring