Quick fix for duplicate (rotten) in containers

Removed some erratic code to fix #4874. The whole function is still a mess, though.