complete rewrite in AngularJS