better thumbnail handling code