working on the amortized version