This method is a bit of a beast. If you can break it up in a way that makes it simpler, please do.

(one code smell rule: if your method has sections like `// Look  up top-level extension type`, maybe that section should be a method.)