javascript refactor