Phase correction algorithm: minimisation phase error
I commend you for using an intuitive algorithm. However, there are already established algorithms with far better performance. Phase recovery algorithms work by filtering the error signal down to zero. An example is the Costas loop structure shown in the figure below for QPSK.

Let's start with phase error detectors (Highlighted in yellow). The arctan detector you used in your example works but it's not typically used in practical systems (The division of imaginary/real may result in overflow if the real quantity is very small). Most systems use the Costas and maximum-likelihood error detectors. Taking BPSK for example. The Costas phase error detector is simply ($real\times imag$). Error detectors are characterized by an S-curve obtained by finding the expectation of the error detector output (For BPSK should be the mean of ($real\times imag$)) for different phase error inputs (phase errors from $-\pi$ to $\pi$ in steps of $\frac{pi}{N}$) as shown below.

Filtering is done by something called the loop filter that can &quot;drive&quot; the error down to zero. The proportional-plus-integrator shown below is widely used at it can drive both static phase error and frequency offset to zero. $K_{n}$ parameters (which depend on the slope of the S-curve) dictate the behavior of the loop.

Regarding examples. There are plenty of examples online such as this one. You could find many more by searching for Costas loop examples online. If interested, you could have a look at GNU Radio tutorials. Plenty of examples too.
And finally, I could suggest some improvement in your simulation. You should consider adding a small frequency offset as well as phase noise to fully represent a real-world scenario.
Good luck.