Significant cleanup of element generation code.