The easy answer is yes, there must be a bug in your code. But your code comes with no explanation of what it does. There is no indication of how you are representing the fields. Really not much to go on, and it is a considerable task for one to sort through all that code.