Refactoring of js code