Fixed complicated for loops