Separate the "find the correct context" part from "substitute in the base type" part. The former can go away completely after a bit of refactoring.