Optimized code using pycharm