Refined query expansion code.