Modified to include logging and some better Phi matrix generation for
larger dimensions