Python 2 compatibility and minor fixes