CHG: Optimised some of the code and added some comments