This is all spiffy but test cases for lines 406-420 in particular would both make the code easier to understand and more awesome. Would it be worth it to split this chunk off into a separate function taking a single `badges` argument, which would be nice and easy to test?

I realize we're under a supertight deadline though so it's ok if it can't be done.