Reimplemented some Coverity fixes from the ArduinoDue branch. Fixes up some small issues in the code.