optimise animate function