moved out funcs out of loops