Replace depreciated code
Add check that $user is set