some cleanup + generate_preview function