refactoring: remove `g :: IORef Int' from many functions