switching from setinterval to requestAnimationFrame plus a little code reorg