Make apply_extends more threadsafe, probably.  Fixes #304.