Refactor to get rid of the global var