Refactoring python mode