logic for delete and insert