Backport loops clean up, close #941