Move function call outside of loops