Refactor animation code