Undefined behavior fixes