Refactor popup into a function.