refactored to remove globals