improved time and space complexity
some comments