Declared some missing variables, and added an isset check

To avoid php notices about undeclared variables and indexes