Improvement on bugfix #2257: should now only remove the array items that were added before.