refactor to get rid of some more globals