fix loop bugs in simple cases