Tested with Python 3, tidy a bit.