refactorize python script