cleanup and fix some bugs in ir reader/writer