Fix to improve handling of element data and reduce memory leaks

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