Calculation of EMA, history filtering and some clean-up