Generic code for ad management