refact some javascript code