this is a translation from C++ to python, with some small changes an optimisations. the coding style isn't pure python. Zurcher Jeremy