move functions out of loops