Thumbnail fixes

some fixes and refactoring for the thumbnailing code

cc @DeepDiver1975