* made several globals local
* reordered the logic slightly to skip unneeded code executions

Signed-off-by: Ession <mail@mathiasjost.com>
* bumped addon version to 5.2.2

Signed-off-by: Ession <mail@mathiasjost.com>