improve space complexity