Refactoring with requestAnimationFrame