Refactored the newCtxWithEventManager function