refactoring to remove global vars