reorganize code using angularjs and callback