Major reorganisation of Javascript.

Change-Id: I4acff02269fd654b5b8b2b61b2649e82ed7e527a