Refactor context assembly as a separate routine