refactor to remove a global variable