moved inside loop