Refactored to remove global variable