do refactoring, cleanup and bugfix

fix bugs related with SETGLOBAL
cleanup + refactoring (thanks to daurnimator and hoelzro)