Fixed the order of element deletion.