Added EMA, various refactors/bugfixes