Fix O(tn) in memory