Refactor generation of packet headers