refactor Packet reader and writer