Modified db query and delete function