Use a KDTree to optimize the process of fitting tiles.
Cache the raw jpeg bytes of the thumbnails, rather than the decompressed images.
Cache calculated mosaics.