Python refactor