Use the available context instead of global state whereever possible.

Change-Id: I75aed2c4d211ed798406aa43531922e99092c13d