Move requestAnimationFrame and fix looping bug