Repacketization code (untested)