rewrite the big analysis loop into CPS style