Python refactoring

Work in progress