Major JS refactor