changed to use request animation frame