Added overflow and underflow detection