Reformatted code, added comments and updated debug messages in doflash function.