javascript refacotering