change to in-place algorithm