Refactored animation code