remove all globals and refactor