added comments + a few optimisations