Reworked animation loop to work with requestAnimationFrame.