Various bug fixes. Updated to work with Python 3.