Terminate the loop early if it's going to overflow; there's no reason to keep riding it out. Also fix use of uninitialized variable.