I... sort of see what's going on here. But rather than continue staring at it I think I'll suggest you add a big comment walking through the logic :-)