refactoring of javascript