Tidied up the code and the documentation. Preliminary Python 3 compatibility.