refactored for better AngularJs support -ab