Added comments and some bug fixes in the JavaScript.