Refactor to avoid global mutable state