fix stupid memory exhaustion bug
avoid potential integer overflows