use function rather than declaring variables in global