Trying to improve some for loops