Refactor popup.js