Refactor to remove globals