Improve showing and hiding animation logic