more cleanup and error handling

raise IOErrors where file handling is done