I'm confused about why this is here. It might very well belong here, but it still isn't obvious why. Since this function is a little complicated, can you comment some of the stuff inside the function so it's more clear what's going on? Other than this I was able to follow most everything, but I had to take more time reading it than I otherwise would if there were comments along the way.