Fix to improve handling of element data

Fix to improve handling of element data and reduce the potential for
memory leaks when frequently adding/removing elements