The problem is you delete one cell without recounting or subtract the number of removed indexes.
The code works great except for 2 things.
* If you delete one item, the one after will be ignored because it was shifted to the position of the first one.
* When the loop reaches the end, it doesn't know that the size has changed. This is the reason it throws an error, you're trying to retrieve values outside of the array range.
Look at the example I gave. Especially the variable deleted_items.