Refactor JS Code