Refactoring delete function