Revert "Put some function calls outside of foreach iteration"

This reverts commit 5cb09af8ada26eedf61418540f8a66128d98da12.