remove some globals, some refactors