Refactor io module to fix all data races