refactored code with read() and write() methods