fix possible underflow bug