refactor to remove global mess