All of these globals scare me. Shouldn't they be stream specific? I haven't read the code in enough detail to know what is going on here, so I could be wrong.