refactored io code