Refactoring: integer overflow check functions and error codes