Move flatten impl to transform module