moved a loop outside a function, less indentation