I was just reading your code and I have a question:
Are the variables names ok?
Maybe you need to name the 'ID' vars different. IDo and IDn for example.
Maybe the problem with the update is that the 'replace' cannot do his job.
If that is an errata...well... try with SO parameters.
Hope it works.