Fix various undefined variable/property errors
Fix unused local variables throughout the codebase