Started refactoring the popup code