AngularJS code rearranged