more loop fixes and tests