Fix reg_write() bug and add debugging helpers.