Fixing "Undefined index and non-object errors" bug