Fixed the previous patch as per your comments

Refactored the loop to avoid the shallow copy and to make clear the behaviour.