refactor and reorganize javascript