javascript: refactor