Avoid creating functions inside loop, properly terminates now