Recursively calling the extend function so that extensions are transitive. There are occasionally redundant selectors, and the order is sometimes wrong, but the results appear to be semantically correct.