Split up the JavaScript code into multiple files and added some comments and small fixes