Fix for-loop bug