Cleanup EEPROM writing code.

Avoid goto/label usage by using a loop instead, remove nesting, improve
and simplify logic, use 8 bit type for attempt counter.