Rewritten in Angular.js