Fixed isValidImage function