Fix issue with changing list while iterating