Some JS code reorganization