Added overflow detection, but it needs to be improved.