Fixed a bug that caused the wrong element to be removed when deleting.