Improved delete function