Refactoring of JS code