code refactoring of JS functions