this is a complicated bit of code. maybe it should have a comment explaining what it's doing?