Space complexity improved.