Added most of the code for initialisation