Improve error handling.
Fix possible overflow.