When removing entries while iterating, use a iterator.
Also correct math to figure out when to remove items.
Fixes #386
Add debug code in case of StackOverflow.
Merge remote-tracking branch 'upstream/mc16' into mc16