refactored animate code