Refactoring to remove global state.