rearranged codes and templating