Clean up of code and added parameter optimisation