Refactor functions in aggregate-directives.js