I don't really understand what this function does, and I read it a few times.. could we pull it out to a util object and put a test around it so it's clear how it works?