Refactored much of extension generation to make it easier to read and adhere to DRY better.