Refactor methods to read and write messages

- generalize file read / write
- simplify transfer
- refactor long methods