Python work: mainly refactoring and cleanup.