Fixed infinite inner loop