Refactoring: remove code duplicate in modules.js