refactor deleting code