Fix some warnings, integer overflow, and array overrun