restruct encoding function