Help me understand the Diffie-Hellman Key Exchange
From what I understand:
Agreed upon numbers: Both parties agree on the value of a large prime number $p$ and a generator $g$.
The users each choose private keys; Alice chooses $a$ and Bob chooses $b$, such that $0<a,b<n$.
The users each calculate public keys: Alice computes $A=g^a\pmod{p}$, while Bob computes $B=g^b\pmod{p}$. Each sends the other their public key.
The users calculate the secret key $k$ as the other person's public key raised to the power of their own private key mod $p$. Alice calculates it as $B^a\pmod{p}$, Bob as $A^b\pmod{p}$.
Example: $p=7, g=4$. Alice chooses $a=2$, Bob chooses $b=3$, Charlie chooses $c=4$. Then the public keys are $A=4^2=16$, and $B=4^3=64$, and $C=4^4=256$.
In that case, Alice and Bob will compute $$k_{ab}=64^2=16^3=4096\equiv1\pmod{7}$$
Alice and Charlie will compute $$k_{ac}=256^2=16^4=65536\equiv2\pmod{7}$$
Questions:
Why does $p$ need to be a prime number?
How does the size of $p$ affect its security?
What's a generator ($g$)?
Why must the private key be less than $p$?
What is mod?
what maths/types of logic formulate the science behind this kind of cryptology?
Also, please correct me if I've made any errors above. Please feel free to offer any additional information that you think will aid in understanding this concept.