Fixes several undefined variables and indexes

I've only tested briefly,  but this should fix a bunch of "undefined" notices that display when WP_DEBUG is on.