fix and clean the io code