Move tests outside of loops for better performance