improving AngularJS code which is used for ngRemote