I'm not too comfortable touching this code, it's very similar to cryptography - you touch it wrong and suddenly it's really weak. You need to be really, really sure about the changes.