Refactor and clean up

Moved last commit's code to popup.js. Refer #5 for details.
Fixes #5, but not closing yet as not tested.