improving space complexity