Fixed error where list was being modified inside an iteration.