Refactored code into begin animation function block