python 3 compatibility and some general improvements