Made domain parameterised.
Also added some more comments etc.