some tweaks and reordering for py 3