refactor javascript