Reworked delete function