Refactorings to remove globals