restructured to be used with AngularJS