Straight cut'n'paste from the old renderer code, so I didn't look too closely. I think that could be cleaned up a bit though, yeah.