restructured add frequency algorithm.