Python 3 compatibility fixes by Tomasz.