Fixed bug in loop generator