Simple refactoring of JS code