Massive JS refactor