Major refactoring
Remove some global variables