extracted popup logic