Factorize phi in a smarter way

By factoring the factors it is calculated with to begin with
Fix GeneralMatrix by reducing it more regularly and using unsigned int