Ah right. I think it downloads number of pages instead of number of images, which is kind of dumb ^^.
For a fix add
if ($match_count -gt 52) { break }
after the line
"$match_count = ($match_count + 1);"
This should stop the loop after downloading 52 images. If there are too few images, just up the number in the if.
Hope that helps.