I coded this from the MT3 code above. It is different than the one that you've posted - that one came from a russian forum as I understand it and isn't based on the MT3 code.
I don't have MT3 to test this for validity - do you? If so, I'd love to know if this does what you are looking for.
Cheers,
CS