Fix #296, don't remove elements with referenced children
Created by: joneuhauser
Before adding an element to the list of to-be-removed-elements, we need to check if a child of this element is in the list of referenced IDs.
Closes #296
Added a unit test; the existing unit tests are working. Also flake-8 doesn't complain about the newly-added code.