Changed rendering code to use requestAnimationFrame