restructuring thumbnail and popup logic