Updated by Marko Luther <marko.luther@gmx.net> to work with Python 3.x, still compatible with 2.x