modified the delete function, which delete allocated memory