Moving delete code outside the lock