Refactor popup script