Fix several bugs with thumbnail generation