Copied a few lines from the Command-line version quad.py. Mostly comments, but also some tiny modifications that might fix some infrequent bugs.