refactoring code to use directives and factory services