Refacted to include AngularJS promises