code refactoring of js functions