Big refactoring of js code to be more OO