corrected delete query and added ability to delete orphans