I solved the problem, which wasn't in the mouse looking code, but in matrix calculations. Well, the camera seems to be working well now. You can see the code here.

If you encounter any bugs, please let me know.