fix incorrect algorithm for negative values in wrap()