delete function with more sphagetti code