Better handling of "this" reference inside class

Less global variables and more encapsulated code