optimise the space complexity
some comments