Code cleanup and Python 3 compatibility.