Fix for "Trying to get property of non-object" notice