refactored animation code to a single for animation loop