Rearrange encoding code