Code optimisation, added comments