factored out a large function in xLambda