fix a tiny bug where imageName would've been assigned to the last file...

...in directory even if it was not on extension list.

also rewrite a bit.