Added comments in JS script + reorganizing it a bit

As discussed in e-mails.