You've got this function getting defined every time through the loop, but it doesn't change at all.