Context and transformers work - 2

Continued from #1100.

Not ready for merge -- opened to ask questions on the code and get some reviews/feedback.
I think these fields are actually used by only one place in the code, so maybe they should be moved there instead?