refactored using underscore.js and my brain