Initial JS Refactor: seems to work for standalone at least