cleaned and organized IO code