Refactor context initialization