refeactored reader/writer