Compatibility fixes for Python 3

Also includes more consistent ordering of query parameters