Popup logic. refactoring