Added some safty checks for entity deletion