Refactor delete function