Viewing previous posts, it makes sense that a debug prior to this code segment may be necessary. It seems rather obvious to me (I may be wrong) that $K may be read incorrectly. Give yourself some debug info on it right after you read it or modify it to make sure it's the value you want.