adding comments and working on structure of js file

At the moment it is not very easy for new developers to contribute to the code, because there are not many comments and everything is changed and on big chunk. This is the start of making it a little bit easy. I would like the code to be split into individual parts so that one can easily find bugs or add new features.

Also if we split the code over more lines, it will make for easier merges with less conflicts.