refactoring to remove more globals