Fix/remove unused/undefined variables and properties in the codebase

I had a look at some suspicious code parts thanks to PhpStorm's inspector. Did my best to double-check all of them, but having someone else look at them would be good. Happy reviewing :-).