Release 3.1 code cleanup

Code cleanup and pep8 fixes