fix bug and restructure js