Added comments and some optimisations