Added some comments and reorganized a bit of the code in popup.js