rewrite logic using Angular.js!