Refactor code slightly to ensure context is always a Context instance