Fixed bug in loop gen