Some IO refactoring