I know this is going to sound basic, but add some breakpoints into areas where the operations are being performed. Then debug the code and add watches to the data variables.
By watching each variable, you will be able to narrow down where the problem is, and then post a more concise and directed query.
At the moment, you have posted too much code for people to engage.