Refactor read and write