Sort message by key and other compatibility fixes for python 2.6