Fixing "property of non-object" error