converted loops to underscore.js functions