Fixing 'Undefined offset 0' and 'Trying to get property of non-object'.