Update main.js

It's dangerous to manipulate array while iterating it.