Fixed overflow/underflow bug