refactored io functions to use io.Reader and io.Writer