Refactor context API.
Add unit tests for context creation.