Analytical expressions working for N=2 case but not for larger sets. Im in the middle of error searching, so code is not very usable atm