Refactored the whole IO logic