Extract out replaceBilinearTermsWithNewVariables, refactor to clean things up.