# Deriving \beta_1 in OLS

I'm studying for an Econometrics exam and going over an old lecture slide (see picture). In it the lecturer is deriving \beta_1 in a basic OLS regression with one variable.

I understand everything up to equation 10 and I am able to calcualte eq.11, but I do not understand why the additions/subtractions need to be made in eq. 11. From what I can tell \beta_1 in 10 is not equal to \beta_1 in 11?

Can someone explain why the calcutations are made in this way? And is equation 10 equal to 11? This is very important for me to know because deriving \beta_1 has been a question in basically every exam.

EDIT: I realize now that this question should probably be posted on the statistics-SE instead. I am not eligible to migrate questions so I would appreciate the help of a moderator for doing this. :)

\begin{align}\tag{11}\sum_{i=1}^n(X_i-\bar{X})(Y_i-\bar{Y}) &=\sum_{i=1}^n (X_iY_i-X_i\bar{Y}-\bar{X}Y_i+\bar{X}\bar{Y})\\ &=\sum_{i=1}^n X_iY_i- \sum_{i=1}^nX_i\bar{Y}- \sum_{i=1}^n\bar{X}Y_i+\sum_{i=1}^n\bar{X}\bar{Y}\\ &=\sum_{i=1}^n X_iY_i- \bar{Y}\sum_{i=1}^nX_i-\bar{X} \sum_{i=1}^nY_i+n\bar{X}\bar{Y}\\ &=\sum_{i=1}^n X_iY_i- \bar{Y}(n\bar{X})-\bar{X} (n\bar{Y})+n\bar{X}\bar{Y}\\ &=\sum_{i=1}^n X_iY_i- n\bar{X}\bar{Y}-n\bar{X}\bar{Y}+n\bar{X}\bar{Y}\tag{B}\\ &=\sum_{i=1}^n X_iY_i- n\bar{X}\bar{Y}\tag{A}\\ &=\sum_{i=1}^n X_iY_i- n \left(\frac{1}{n}\sum_{i=1}^nX_i\right)\left(\frac{1}{n}\sum_{i=1}^nY_i\right)\\ &=\sum_{i=1}^n X_iY_i- \frac{1}{n}\left(\sum_{i=1}^nX_i\right)\left(\sum_{i=1}^nY_i\right) \tag{10}\\ \end{align}
The term $$\bar{X}\sum_{i=1}^nY_i=n\bar{X}\bar{Y}$$ is added and subtracted when going from $$(\text{A})$$ to $$(\text{B})$$.