Move context setup code to helper library