I think I've fixed this -- when you do a future review of this branch, please check this function again.